/*************************************************************************
 > File Name: libdatabase.c
 > Author: LiuTianpeng
 > Mail: ltpforever@163.com 
 > Created Time: 2017年07月24日 星期一 10时07分24秒
 ************************************************************************/

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <time.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <dirent.h>
#include <signal.h>
#include <pthread.h>
#include <fcntl.h>
#include <strings.h>
#include <semaphore.h>
#include <ctype.h>

#include "database.h"
#include "sqlite3.h"
#include "ret.h"
#include "offset.h"
#include "codetable.h"

sqlite3* tr_db = NULL; //交易记录
sqlite3* err_db = NULL; //故障记录
sqlite3* price_db = NULL; //计费模型
sqlite3* config_db = NULL; //系统配置
sqlite3* card_db = NULL; //管理卡
sqlite3* blacklistCard_db = NULL; //黑名单
sqlite3* guninfo_db=NULL;//充电枪
sqlite3* code_db = NULL;//故障码

#define TRAN_MAX_NUM  1000  //每个交易记录数据库交易记录最大条数
#define ERROR_MAX_NUM 2000 //每个故障数据库故障记录最大条数


#define TRAN_RECORD_TABLE_NW  "tagAccount"
#define TRAN_RECORD_TABLE_XJ  "tagAccount_xj"
#define TRAN_RECORD_TABLE_YCT  "tagAccount_yct"
#define TABLE_GUNINFO_NAME "tagGunInfo"
#define TABLE_YXCODE "tagYx"
#define TABLE_STOPREASON "tagStopReason"
#define TABLE_XJ_PRICE_MODEL "tagPriceModel_xj"

enum dbType {
	TRAN_DB_TYPE = 1,
	ERROR_DB_TYPE,
	PRICE_DB_TYPE,
	CONFIG_DB_TYPE,
	CARD_DB_TYPE,
	BLACKLIST_CARD_TYPE,
	GUNINFO_DB_TYPE,
	CODE_DB_TYPE,
};

enum PM_TYPE{
	XJ_POWER_RATE_TYPE=1,
	XJ_SERVICE_RATE_TYPE=2,
};
#define DEBUG

static int creat_tran_record_db(void);
static int creat_error_record_db(void);
static int set_CD_value(int *ncolnum, int *index, char **pResult, void *tr);
static int insert_tran_record(void* tr);

static db_register dbType = { TABLE_NW, TABLE_NW, TABLE_NW };

//检测数据库是否存在,0存在
static int file_is_exist(const char *pathname, char db_type) {
	char SQL_cmd[128] = { 0 };
	char *pErrMsg = NULL;

	if (access(pathname, F_OK) != 0) {
		printf("##libdatabase## %s no exist!\n", pathname);
		return ERROR_FAIL;
	}

	if ((db_type == TRAN_DB_TYPE)
			&& (strcmp(pathname, TRAN_RECORD_PATH) == 0)) {
		if (tr_db == NULL) {
			if (sqlite3_open(TRAN_RECORD_PATH, &tr_db) != SQLITE_OK) {
				printf("##libdatabase##connect tcuaccount.db fail!\n");
				sqlite3_close(tr_db);
				tr_db = NULL;
				return ERROR_FAIL;
			}
		}

		if (dbType.db_tran_type == TABLE_XJ)
			sprintf(SQL_cmd, "select * from %s  limit 0;",
					TRAN_RECORD_TABLE_XJ);
		else if (dbType.db_tran_type == TABLE_NW)
			sprintf(SQL_cmd, "select * from %s  limit 0;",
					TRAN_RECORD_TABLE_NW);
		else if (dbType.db_tran_type == TABLE_YCT)
			sprintf(SQL_cmd, "select * from %s  limit 0;",
					TRAN_RECORD_TABLE_YCT);

		if (sqlite3_exec(tr_db, SQL_cmd, 0, 0, &pErrMsg) != SQLITE_OK) {
			printf("##libdatabase## Without table-->%s!\n", SQL_cmd);
			sqlite3_free(pErrMsg);
			sqlite3_close(tr_db);
			tr_db = NULL;
			return ERROR_FAIL;
		}
	} else if ((db_type == ERROR_DB_TYPE)
			&& (strcmp(pathname, ERROR_RECORD_PATH) == 0)) {
		if (err_db == NULL) {
			if (sqlite3_open(ERROR_RECORD_PATH, &err_db) != SQLITE_OK) {
				printf("##libdatabase## connect tcuerror.db fail! \n");
				sqlite3_close(err_db);
				err_db = NULL;
				return ERROR_FAIL;
			}
		}

		sprintf(SQL_cmd, "select * from tagError limit 0;");
		if (sqlite3_exec(err_db, SQL_cmd, 0, 0, &pErrMsg) != SQLITE_OK) {
			printf("##libdatabase## Without table--->tagError!\n");
			sqlite3_free(pErrMsg);
			sqlite3_close(err_db);
			err_db = NULL;
			return ERROR_FAIL;
		}
	} else if ((db_type == CONFIG_DB_TYPE)
			&& (strcmp(pathname, CONFIG_FILE_PATH) == 0)) {
		if (config_db == NULL) {
			if (sqlite3_open(CONFIG_FILE_PATH, &config_db) != SQLITE_OK) {
				printf("##libdatabase## connect tcuconfig.db fail!\n");
				sqlite3_close(config_db);
				config_db = NULL;
				return ERROR_FAIL;
			}

		}

		sprintf(SQL_cmd, "select * from tagConfig limit 0;");
		if (sqlite3_exec(config_db, SQL_cmd, 0, 0, &pErrMsg) != SQLITE_OK) {
			printf("##libdatabase## Without table--->tagConfig!\n");
			sqlite3_free(pErrMsg);
			sqlite3_close(config_db);
			config_db = NULL;
			return ERROR_FAIL;
		}
	} else if ((db_type == PRICE_DB_TYPE)
			&& (strcmp(pathname, PRICE_MODE_PATH) == 0)) {
		if (price_db == NULL) {
			if (sqlite3_open(PRICE_MODE_PATH, &price_db) != SQLITE_OK) {
				printf("##libdatabase## connect tcuprice.db fail!\n");
				sqlite3_close(price_db);
				price_db = NULL;
				return ERROR_FAIL;
			}
		}

		if (dbType.db_tran_type == TABLE_XJ)
			sprintf(SQL_cmd, "select * from %s  limit 0;",TABLE_XJ_PRICE_MODEL);

		if (sqlite3_exec(price_db, SQL_cmd, 0, 0, &pErrMsg) != SQLITE_OK) {
			printf("##libdatabase## Without table--->%s!\n",TABLE_XJ_PRICE_MODEL);
			sqlite3_free(pErrMsg);
			sqlite3_close(price_db);
			price_db = NULL;
			return ERROR_FAIL;
		}

	} else if ((db_type == CARD_DB_TYPE)
			&& (strcmp(pathname, CARD_PATH) == 0)) {
		if (card_db == NULL) {
			if (sqlite3_open(CARD_PATH, &card_db) != SQLITE_OK) {
				printf("##libdatabase## connect tcucard.db fail!\n");
				sqlite3_close(card_db);
				card_db = NULL;
				return ERROR_FAIL;
			}
		}

		sprintf(SQL_cmd, "select * from tagCard limit 0;");
		if (sqlite3_exec(card_db, SQL_cmd, 0, 0, &pErrMsg) != SQLITE_OK) {
			printf("##libdatabase## Without table--->tagCard\n");
			sqlite3_free(pErrMsg);
			sqlite3_close(card_db);
			card_db = NULL;
			return ERROR_FAIL;
		}
	} else if ((db_type == BLACKLIST_CARD_TYPE)  && (strcmp(pathname, BLACKLIST_CARD_PATH) == 0)) {
		if (blacklistCard_db == NULL) {
			if (sqlite3_open(BLACKLIST_CARD_PATH, &blacklistCard_db) != SQLITE_OK) {
				printf("##libdatabase## connect tcublacklistcard.db fail!\n");
				sqlite3_close(blacklistCard_db);
				blacklistCard_db = NULL;
				return ERROR_FAIL;
			}
		}

		sprintf(SQL_cmd, "select * from tagBlackCard limit 0;");
		if (sqlite3_exec(blacklistCard_db, SQL_cmd, 0, 0, &pErrMsg) != SQLITE_OK) {
			printf("##libdatabase## Without table--->tagBlackCard\n");
			sqlite3_free(pErrMsg);
			sqlite3_close(blacklistCard_db);
			blacklistCard_db = NULL;
			return ERROR_FAIL;
		}
	} else if((db_type == GUNINFO_DB_TYPE) &&  (strcmp(pathname, GUNINFO_PATH) == 0)){
		if(guninfo_db == NULL){
			if (sqlite3_open(GUNINFO_PATH,	&guninfo_db) != SQLITE_OK) {
				printf("##libdatabase## connect tcuguninfo.db fail!\n");
				sqlite3_close(guninfo_db);
				guninfo_db = NULL;
				return ERROR_FAIL;
			}
		}

		sprintf(SQL_cmd, "select * from %s limit 0;",TABLE_GUNINFO_NAME);
		if (sqlite3_exec(guninfo_db, SQL_cmd, 0, 0, &pErrMsg) != SQLITE_OK) {
			printf("##libdatabase## Without table--->%s\n",TABLE_GUNINFO_NAME);
			sqlite3_free(pErrMsg);
			sqlite3_close(guninfo_db);
			guninfo_db = NULL;
			return ERROR_FAIL;
		}

	}
	else if((db_type == CODE_DB_TYPE) &&  (strcmp(pathname, CODE_PATH) == 0)){
		if(code_db == NULL){
			if (sqlite3_open(CODE_PATH,	&code_db) != SQLITE_OK) {
				printf("##libdatabase## connect tcucode.db fail!\n");
				sqlite3_close(code_db);
				code_db = NULL;
				return ERROR_FAIL;
			}
		}

		sprintf(SQL_cmd, "select * from %s limit 0;",TABLE_YXCODE);
		if (sqlite3_exec(code_db, SQL_cmd, 0, 0, &pErrMsg) != SQLITE_OK) {
			printf("##libdatabase## Without table--->%s\n",TABLE_YXCODE);
			sqlite3_free(pErrMsg);
			sqlite3_close(code_db);
			code_db = NULL;
			return ERROR_FAIL;
		}

		sprintf(SQL_cmd, "select * from %s limit 0;",TABLE_STOPREASON);
		if (sqlite3_exec(code_db, SQL_cmd, 0, 0, &pErrMsg) != SQLITE_OK) {
			printf("##libdatabase## Without table--->%s\n",TABLE_STOPREASON);
			sqlite3_free(pErrMsg);
			sqlite3_close(code_db);
			code_db = NULL;
			return ERROR_FAIL;
		}

	}else {
		printf("##libdatabase## test file exist inpara error!\n");
		return ERROR_INPARA;
	}

	//	printf("##libdatabase## %s is exist and connect success!\n", pathname);

	sqlite3_free(pErrMsg);
	return ERROR_OK;
}

static int protocol_database_manager(const char *pathname, char db_type) {
	char SQL_cmd[128] = { 0 };
	char sys_cmd[128] = { 0 };
	char *pErrMsg = NULL;
	int ret = 0;
	int nrow = 0, ncolnum = 0;
	char **pResult = NULL;
	DIR *dp = NULL;

	if (pathname == NULL) {
		printf("##libdatabase## %s fail, due to para is NULL!!!\n", __func__);
		return ERROR_FAIL;
	}

	if ((dp = opendir(CACHE_PATH)) == NULL)
		if (mkdir(CACHE_PATH, S_IRWXU | S_IRWXG | S_IRWXO) != 0) {
			tr_db = NULL;
			closedir(dp);
			return ERROR_FAIL;
		}
	closedir(dp);

	if (db_type == TRAN_DB_TYPE && (strcmp(pathname, TRAN_RECORD_PATH) == 0)) {
		if (dbType.db_tran_type == TABLE_NW)
			sprintf(SQL_cmd, "select * from %s;", TRAN_RECORD_TABLE_NW);
		else if (dbType.db_tran_type == TABLE_YCT)
			sprintf(SQL_cmd, "select * from %s;", TRAN_RECORD_TABLE_YCT);
		else
			sprintf(SQL_cmd, "select * from %s;", TRAN_RECORD_TABLE_XJ);

		ret = sqlite3_get_table(tr_db, SQL_cmd, &pResult, &nrow, &ncolnum,
				&pErrMsg);
		if (ret == SQLITE_OK)
			if (nrow >= (TRAN_MAX_NUM + 1)) {
				sqlite3_close(tr_db);
				sprintf(sys_cmd, "mv %s %s/%s", TRAN_RECORD_PATH, CACHE_PATH,
						TRAN_CACHE_FILE);
				system(sys_cmd);
				printf("%s\n", sys_cmd);
				tr_db = NULL; //搬移之后将原始指针置空

				//将搬移后的数据库里含有未上传的交易记录插在新数据库中
				sqlite3* tr_bak_db = NULL;
				char tr_bak_path[64] = { 0 };
				int ret1 = 0;
				tran_record offline_tr_nw;
				tran_record_xj offline_tr_xj;
				int i = 0, index = 0, size = 0;

				sprintf(tr_bak_path, "%s/%s", CACHE_PATH, TRAN_CACHE_FILE);
				if (sqlite3_open(tr_bak_path, &tr_bak_db) != SQLITE_OK) {
					printf(
							"##libdatabase##manager _tcuaccount.db fail,due to can't connect %s\n",
							tr_bak_path);
					sqlite3_close(tr_bak_db);
					tr_bak_db = NULL;
					return ERROR_FAIL;
				}
				ret1 = file_is_exist(TRAN_RECORD_PATH, TRAN_DB_TYPE);
				if (ret1 == ERROR_INPARA)
					return ERROR_FAIL;
				else if (ret1 == ERROR_FAIL) {
					if (creat_tran_record_db() != ERROR_OK)
						return ERROR_FAIL;
				}

				if (tr_db == NULL)
					if (sqlite3_open(TRAN_RECORD_PATH, &tr_db) != SQLITE_OK) {
						sqlite3_close(tr_db);
						tr_db = NULL;
						return ERROR_FAIL;
					}

				if (dbType.db_tran_type == TABLE_NW)
					sprintf(SQL_cmd,
							"select * from %s where flag_finished in  (%d,%d,%d);",
							TRAN_RECORD_TABLE_NW, TRAN_START, TRAN_OFFLINE,
							TRAN_TEM);
				else if (dbType.db_tran_type == TABLE_YCT)
					sprintf(SQL_cmd,
							"select * from %s where flag_finished in  (%d,%d,%d)  or flag_card_settled=%d;",
							TRAN_RECORD_TABLE_YCT, TRAN_START, TRAN_OFFLINE,
							TRAN_TEM, 0x55);
				else if (dbType.db_tran_type == TABLE_XJ)
					sprintf(SQL_cmd,
							"select * from %s where flag_finished in  (%d,%d,%d);",
							TRAN_RECORD_TABLE_XJ, TRAN_START, TRAN_OFFLINE,
							TRAN_TEM);

				if (sqlite3_get_table(tr_bak_db, SQL_cmd, &pResult, &nrow,
						&ncolnum, &pErrMsg) != SQLITE_OK) {
					printf(
							"##libdatabase## get offline _tcuaccount.db get table info fail!--->%s\n",
							pErrMsg);
					sqlite3_free(pErrMsg);
					sqlite3_free_table(pResult);
					sqlite3_close(tr_bak_db);
					tr_bak_db = NULL;

					return ERROR_FAIL;
				}

				if (nrow > 0) {
					index = ncolnum;
					if (dbType.db_tran_type == TABLE_NW
							|| dbType.db_tran_type == TABLE_YCT) {
						size = sizeof(offline_tr_nw);
						for (i = 0; i < nrow; i++) {
							memset(&offline_tr_nw, 0, size);
							ret = set_CD_value(&ncolnum, &index, pResult,
									&offline_tr_nw);
							if (ret != ERROR_OK && ret != ERROR_INPARA) {
								sqlite3_free(pErrMsg);
								sqlite3_free_table(pResult);
								return ERROR_FAIL;
							} else {
								insert_tran_record(&offline_tr_nw); //2019-01-04
							}
						}
					} else if (dbType.db_tran_type == TABLE_XJ) {
						size = sizeof(offline_tr_xj);
						for (i = 0; i < nrow; i++) {
							memset(&offline_tr_xj, 0, size);
							ret = set_CD_value(&ncolnum, &index, pResult,
									&offline_tr_xj);
							if (ret != ERROR_OK && ret != ERROR_INPARA) {
								sqlite3_free(pErrMsg);
								sqlite3_free_table(pResult);
								return ERROR_FAIL;
							} else {
								insert_tran_record(&offline_tr_xj); //2019-01-04
							}
						}
					}
				}

			}
	} else if (db_type == ERROR_DB_TYPE
			&& (strcmp(pathname, ERROR_RECORD_PATH) == 0)) {
		sprintf(SQL_cmd, "select * from tagError;");
		ret = sqlite3_get_table(err_db, SQL_cmd, &pResult, &nrow, &ncolnum,
				&pErrMsg);
		if (ret == SQLITE_OK)
			if (nrow >= (ERROR_MAX_NUM + 1)) {
				sprintf(sys_cmd, "mv %s %s/%s", ERROR_RECORD_PATH, CACHE_PATH,
						ERROR_CACHE_FILE);
				system(sys_cmd);
				err_db = NULL;
			}
	} else {
		printf("##libdatabase## manager %s fail,due to no function!!!\n",
				pathname);
		return ERROR_INPARA;
	}

	sqlite3_free(pErrMsg);
	sqlite3_free_table(pResult);
	printf("##libdatabase## manager %s---%s\n", pathname, pErrMsg);

	return ret;
}

int callback_func(void* pvoid, int colnum, char **coldata, char **colname) {
	int i = 0;

	printf("##libdatabase## total colnum is %d\n", colnum);
	for (i = 0; i < colnum; i++)
		printf("%s = %s\n", colname[i], coldata[i] ? coldata[i] : NULL);
	printf("----end\n");
	return ERROR_OK;
}

//创建交易记录数据库 0创建成功
static int creat_tran_record_db(void) {
	DIR *dp = NULL;
	char *pErrMsg = NULL;
	char SQL_cmd[2048] = { 0 };

	if (dbType.db_tran_type == TABLE_NW) {
		sprintf(SQL_cmd,
				"create table if not exists %s([guntab] char,[flag_finished] char,[serialnum] varchar(33) PRIMARY KEY,"
				"[starttime] varchar(15), [endtime] varchar(15), [charge_duration] int, [payCard] varchar(17),"
				" [userId] varchar(17), [cardId] varchar(17), [modelID] varchar(17), [stop_reason] char, [billbytm] char,"
				" [w_start_0] int,  [w_start_1] int, [w_start_2] int, [w_start_3] int, [w_start_4] int,"
				" [w_end_0] int, [w_end_1] int, [w_end_2] int, [w_end_3] int, [w_end_4] int,"
				" [w_0] int, [w_1] int, [w_2] int, [w_3] int,[w_4] int, [cost_elec_0] int, [cost_elec_1] int, [cost_elec_2] int, "
				"[cost_elec_3] int, [cost_elec_4] int,"
				" [cost_server] int, [cost_Parking] int,"
				"[rateprice_0] int, [rateprice_1] int, [rateprice_2] int, [rateprice_3] int, [rateprice_4] int,"
				" [rateprice_5] int, [rateprice_6] int, [carlogo] varchar(35) );",
				TRAN_RECORD_TABLE_NW);
	} else if (dbType.db_tran_type == TABLE_YCT) {
		sprintf(SQL_cmd,
				"create table if not exists %s([guntab] char,[flag_finished] char,[serialnum] varchar(33) PRIMARY KEY,"
				"[starttime] varchar(15), [endtime] varchar(15), [charge_duration] int, [payCard] varchar(17),"
				" [userId] varchar(17), [cardId] varchar(17), [modelID] varchar(17), [stop_reason] char, [billbytm] char,"
				" [w_start_0] int,  [w_start_1] int, [w_start_2] int, [w_start_3] int, [w_start_4] int,"
				" [w_end_0] int, [w_end_1] int, [w_end_2] int, [w_end_3] int, [w_end_4] int,"
				" [w_0] int, [w_1] int, [w_2] int, [w_3] int,[w_4] int, [cost_elec_0] int, [cost_elec_1] int, [cost_elec_2] int, "
				"[cost_elec_3] int, [cost_elec_4] int, "
				" [cost_server] int, [cost_Parking] int, "
				"[rateprice_0] int, [rateprice_1] int, [rateprice_2] int, [rateprice_3] int, [rateprice_4] int, "
				"[rateprice_5] int, [rateprice_6] int, [carlogo] varchar(35) , [flag_card_settled] int, [tac] varchar(9), "
				"[logic_cardNum] varchar(17), [phy_cardNum] varchar(17), [ticket_trade_count] int, [trade_money] int, "
				"[balance] int);", TRAN_RECORD_TABLE_YCT);
	} else if (dbType.db_tran_type == TABLE_XJ) {
		/*
		sprintf(SQL_cmd,
				"create table if not exists %s([guntab] char,[flag_finished] char,[serialnum] varchar(65) PRIMARY KEY,"
				"[starttime] varchar(15), [endtime] varchar(15), [charge_duration] int, [start_soc] int, [end_soc] int,"
				"[userId] varchar(17), [cardId] varchar(17), [stop_reason] char, [w_start] int, [w_end] int,"
				"[w_0] int, [w_1] int, [w_2] int, [w_3] int, [w_4] int, [w_5] int, [w_6] int, [w_7] int, [w_8] int, [w_9] int,"
				"[w_10] int, [w_11] int, [w_12] int, [w_13] int, [w_14] int, [w_15] int, [w_16] int, [w_17] int, [w_18] int, [w_19] int,"
				"[w_20] int, [w_21] int, [w_22] int, [w_23] int, [w_24] int, [w_25] int, [w_26] int, [w_27] int, [w_28] int, [w_29] int,"
				"[w_30] int, [w_31] int, [w_32] int, [w_33] int, [w_34] int, [w_35] int, [w_36] int, [w_37] int, [w_38] int, [w_39] int,"
				"[w_40] int, [w_41] int, [w_42] int, [w_43] int, [w_44] int, [w_45] int, [w_46] int, [w_47] int, [w_48] int,"
				"[cost_elec] int, [cost_server] int, [carlogo] varchar(35), [billmode] char, [chargevalue] int,"
				"[startmode] char, [g_stop_detail] int );",
				TRAN_RECORD_TABLE_XJ);
		 */

		sprintf(SQL_cmd,
				"create table if not exists %s([guntab] INTEGER,[flag_finished] INTEGER,[serialnum] varchar(65) PRIMARY KEY,"
				"[starttime] varchar(15), [endtime] varchar(15), [charge_duration] INTEGEREGER, [start_soc] INTEGEREGER, [end_soc] INTEGER,"
				"[userId] varchar(17), [cardId] varchar(17), [stop_reason] INTEGER, [w_start] INTEGER, [w_end] INTEGER,"
				"[w_0] INTEGER, [w_1] INTEGER, [w_2] INTEGER, [w_3] INTEGER, [w_4] INTEGER, [w_5] INTEGER, [w_6] INTEGER, [w_7] INTEGER, [w_8] INTEGER, [w_9] INTEGER,"
				"[w_10] INTEGER, [w_11] INTEGER, [w_12] INTEGER, [w_13] INTEGER, [w_14] INTEGER, [w_15] INTEGER, [w_16] INTEGER, [w_17] INTEGER, [w_18] INTEGER, [w_19] INTEGER,"
				"[w_20] INTEGER, [w_21] INTEGER, [w_22] INTEGER, [w_23] INTEGER, [w_24] INTEGER, [w_25] INTEGER, [w_26] INTEGER, [w_27] INTEGER, [w_28] INTEGER, [w_29] INTEGER,"
				"[w_30] INTEGER, [w_31] INTEGER, [w_32] INTEGER, [w_33] INTEGER, [w_34] INTEGER, [w_35] INTEGER, [w_36] INTEGER, [w_37] INTEGER, [w_38] INTEGER, [w_39] INTEGER,"
				"[w_40] INTEGER, [w_41] INTEGER, [w_42] INTEGER, [w_43] INTEGER, [w_44] INTEGER, [w_45] INTEGER, [w_46] INTEGER, [w_47] INTEGER, [w_48] INTEGER,"
				"[cost_elec] INTEGER, [cost_server] INTEGER, [carlogo] varchar(35), [billmode] INTEGER, [chargevalue] INTEGER,"
				"[startmode] INTEGER, [g_stop_detail] INTEGER );",
				TRAN_RECORD_TABLE_XJ);

	}

	if ((dp = opendir(DB_DIR_PATH)) == NULL)
		if (mkdir(DB_DIR_PATH, S_IRWXU | S_IRWXG | S_IRWXO) != 0) {
			tr_db = NULL;
			closedir(dp);
			return ERROR_FAIL;
		}
	closedir(dp);

	if (access(TRAN_RECORD_PATH, F_OK) != 0) {
		if (creat(TRAN_RECORD_PATH, O_WRONLY | O_TRUNC) == -1) {
			tr_db = NULL;
			return ERROR_FAIL;
		}
		chmod(TRAN_RECORD_PATH, S_IRWXU | S_IRWXG | S_IRWXO);
	}

	if (tr_db == NULL)
		if (sqlite3_open(TRAN_RECORD_PATH, &tr_db) != SQLITE_OK) {
			sqlite3_close(tr_db);
			tr_db = NULL;

			return ERROR_FAIL;
		}
#ifdef DEBUG
	printf("##libdatabase##: %s\n", SQL_cmd);
#endif

	if (sqlite3_exec(tr_db, SQL_cmd, 0, 0, &pErrMsg) != SQLITE_OK) {
		fprintf(stderr,
				"##libdatabase## Created tcuaccount.db database fail: %s\n",
				pErrMsg);
		sqlite3_free(pErrMsg);
		sqlite3_close(tr_db);
		tr_db = NULL;

		return ERROR_FAIL;
	}

	sqlite3_free(pErrMsg);
	printf("##libdatabase## Created tcuaccount.db database success !\n");
	return ERROR_OK;
}

//创建交易记录 0创建成功
static int insert_tran_record(void* ptr) {
	char *pErrMsg = NULL;
	char SQL_cmd[1024] = { 0 };
	char m_serialnum[65] = { 0 };
	char m_starttime[15] = { 0 };
	char m_endtime[15] = { 0 };
	char m_payCard[17] = { 0 };
	char m_userId[17] = { 0 };
	char m_cardId[17] = { 0 };
	char m_modelID[17] = { 0 };
	char m_carlogo[35] = { 0 };
	int i = 0, len = 0;
	char m_tmp[3] = { 0 };

	tran_record *tr_nw = NULL;
	tran_record_xj *tr_xj = NULL;

	if (!ptr) {
		printf("##libdatabase## %s fail, due to para is NULL!!!\n", __func__);
		return ERROR_INPARA;
	}

	if (dbType.db_tran_type == TABLE_NW || dbType.db_tran_type == TABLE_YCT) {
		tr_nw = (tran_record*) ptr;

		len = sizeof(tr_nw->serialnum) / sizeof(tr_nw->serialnum[0]);
		for (i = 0; i < len; i++) {
			memset(m_tmp, 0, sizeof(m_tmp));
			sprintf(m_tmp, "%02x", tr_nw->serialnum[i]);
			strcat(m_serialnum, m_tmp);
		}

		len = sizeof(tr_nw->starttime) / sizeof(tr_nw->starttime[0]);
		for (i = 0; i < len; i++) {
			memset(m_tmp, 0, sizeof(m_tmp));
			sprintf(m_tmp, "%02x", tr_nw->starttime[i]);
			strcat(m_starttime, m_tmp);
		}

		len = sizeof(tr_nw->endtime) / sizeof(tr_nw->endtime[0]);
		for (i = 0; i < len; i++) {
			memset(m_tmp, 0, sizeof(m_tmp));
			sprintf(m_tmp, "%02x", tr_nw->endtime[i]);
			strcat(m_endtime, m_tmp);
		}

		len = sizeof(tr_nw->payCard) / sizeof(tr_nw->payCard[0]);
		for (i = 0; i < len; i++) {
			memset(m_tmp, 0, sizeof(m_tmp));
			sprintf(m_tmp, "%02x", tr_nw->payCard[i]);
			strcat(m_payCard, m_tmp);
		}

		len = sizeof(tr_nw->userId) / sizeof(tr_nw->userId[0]);
		for (i = 0; i < len; i++) {
			memset(m_tmp, 0, sizeof(m_tmp));
			sprintf(m_tmp, "%02x", tr_nw->userId[i]);
			strcat(m_userId, m_tmp);
		}

		len = sizeof(tr_nw->cardId) / sizeof(tr_nw->cardId[0]);
		for (i = 0; i < len; i++) {
			memset(m_tmp, 0, sizeof(m_tmp));
			sprintf(m_tmp, "%02x", tr_nw->cardId[i]);
			strcat(m_cardId, m_tmp);
		}

		len = sizeof(tr_nw->modelID) / sizeof(tr_nw->modelID[0]);
		for (i = 0; i < len; i++) {
			memset(m_tmp, 0, sizeof(m_tmp));
			sprintf(m_tmp, "%02x", tr_nw->modelID[i]);
			strcat(m_modelID, m_tmp);
		}

		len = sizeof(tr_nw->carlogo) / sizeof(tr_nw->carlogo[0]);
		for (i = 0; i < len; i++) {
			memset(m_tmp, 0, sizeof(m_tmp));
			sprintf(m_tmp, "%02x", tr_nw->carlogo[i]);
			strcat(m_carlogo, m_tmp);
		}

		if (dbType.db_tran_type == TABLE_NW) {
			sprintf(SQL_cmd,
					"insert into %s([guntab],[flag_finished],[serialnum],[starttime],[endtime],[charge_duration],"
					"[payCard],[userId],[cardId],[modelID],[stop_reason],[billbytm],[w_start_0],[w_start_1],[w_start_2],[w_start_3],"
					"[w_start_4],[w_end_0],[w_end_1],[w_end_2],[w_end_3],[w_end_4],[w_0],[w_1],[w_2],[w_3],[w_4],[cost_elec_0],"
					"[cost_elec_1],[cost_elec_2],[cost_elec_3],[cost_elec_4],[cost_server],[cost_Parking],"
					"[rateprice_0],[rateprice_1],[rateprice_2],[rateprice_3],[rateprice_4],[rateprice_5],[rateprice_6],[carlogo]) "
					"values(%d, %d, \"%s\", \"%s\", \"%s\", %d, \"%s\", \"%s\", \"%s\",\"%s\" ,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,"
					"%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,\"%s\");",
					TRAN_RECORD_TABLE_NW, tr_nw->guntab, tr_nw->flag_finished,
					m_serialnum, m_starttime, m_endtime, tr_nw->charge_duration,
					m_payCard, m_userId, m_cardId, m_modelID,
					tr_nw->stop_reason, tr_nw->billbytm, tr_nw->w_start[0],
					tr_nw->w_start[1], tr_nw->w_start[2], tr_nw->w_start[3],
					tr_nw->w_start[4], tr_nw->w_end[0], tr_nw->w_end[1],
					tr_nw->w_end[2], tr_nw->w_end[3], tr_nw->w_end[4],
					tr_nw->w[0], tr_nw->w[1], tr_nw->w[2], tr_nw->w[3],
					tr_nw->w[4], tr_nw->cost_elec[0], tr_nw->cost_elec[1],
					tr_nw->cost_elec[2], tr_nw->cost_elec[3],
					tr_nw->cost_elec[4], tr_nw->cost_server,
					tr_nw->cost_Parking, tr_nw->rateprice[0],
					tr_nw->rateprice[1], tr_nw->rateprice[2],
					tr_nw->rateprice[3], tr_nw->rateprice[4],
					tr_nw->rateprice[5], tr_nw->rateprice[6], m_carlogo);
		} else {
			char m_tac[9] = { 0 };
			char m_logicCard[17] = { 0 };
			char m_phyCard[17] = { 0 };

			len = sizeof(tr_nw->TAC) / sizeof(tr_nw->TAC[0]);
			for (i = 0; i < len; i++) {
				memset(m_tmp, 0, sizeof(m_tmp));
				sprintf(m_tmp, "%02x", tr_nw->TAC[i]);
				strcat(m_tac, m_tmp);
			}

			len = sizeof(tr_nw->yct_nosettle_trade.logic_cardNum) /sizeof(tr_nw->yct_nosettle_trade.logic_cardNum[0]);
			for (i = 0; i < len; i++) {
				memset(m_tmp, 0, sizeof(m_tmp));
				sprintf(m_tmp, "%02x",
						tr_nw->yct_nosettle_trade.logic_cardNum[i]);
				strcat(m_logicCard, m_tmp);
			}

			len = sizeof(tr_nw->yct_nosettle_trade.logic_cardNum) / sizeof(tr_nw->yct_nosettle_trade.cardNum[0]);
			for (i = 0; i < len; i++) {
				memset(m_tmp, 0, sizeof(m_tmp));
				sprintf(m_tmp, "%02x", tr_nw->yct_nosettle_trade.cardNum[i]);
				strcat(m_phyCard, m_tmp);
			}

			sprintf(SQL_cmd,
					"insert into %s([guntab],[flag_finished],[serialnum],[starttime],[endtime],[charge_duration],"
					"[payCard],[userId],[cardId],[modelID],[stop_reason],[billbytm],[w_start_0],[w_start_1],[w_start_2],[w_start_3],"
					"[w_start_4],[w_end_0],[w_end_1],[w_end_2],[w_end_3],[w_end_4],[w_0],[w_1],[w_2],[w_3],[w_4],[cost_elec_0],"
					"[cost_elec_1],[cost_elec_2],[cost_elec_3],[cost_elec_4],[cost_server],[cost_Parking],"
					"[rateprice_0],[rateprice_1],[rateprice_2],[rateprice_3],[rateprice_4],[rateprice_5],[rateprice_6],[carlogo],"
					"[flag_card_settled],[tac],[logic_cardNum],[phy_cardNum],[ticket_trade_count],[trade_money],[balance]) "
					"values(%d, %d, \"%s\", \"%s\", \"%s\", %d, \"%s\", \"%s\", \"%s\",\"%s\" ,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,"
					"%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,\"%s\","
					"%d,\"%s\",\"%s\",\"%s\",%d,%d,%d);",
					TRAN_RECORD_TABLE_YCT, tr_nw->guntab, tr_nw->flag_finished,
					m_serialnum, m_starttime, m_endtime, tr_nw->charge_duration,
					m_payCard, m_userId, m_cardId, m_modelID,
					tr_nw->stop_reason, tr_nw->billbytm, tr_nw->w_start[0],
					tr_nw->w_start[1], tr_nw->w_start[2], tr_nw->w_start[3],
					tr_nw->w_start[4], tr_nw->w_end[0], tr_nw->w_end[1],
					tr_nw->w_end[2], tr_nw->w_end[3], tr_nw->w_end[4],
					tr_nw->w[0], tr_nw->w[1], tr_nw->w[2], tr_nw->w[3],
					tr_nw->w[4], tr_nw->cost_elec[0], tr_nw->cost_elec[1],
					tr_nw->cost_elec[2], tr_nw->cost_elec[3],
					tr_nw->cost_elec[4], tr_nw->cost_server,
					tr_nw->cost_Parking, tr_nw->rateprice[0],
					tr_nw->rateprice[1], tr_nw->rateprice[2],
					tr_nw->rateprice[3], tr_nw->rateprice[4],
					tr_nw->rateprice[5], tr_nw->rateprice[6], m_carlogo,
					tr_nw->flag_card_settled, m_tac, m_logicCard, m_phyCard,
					tr_nw->yct_nosettle_trade.ticket_trade_count,
					tr_nw->yct_nosettle_trade.trade_money,
					tr_nw->yct_nosettle_trade.balance);
		}
	} else if (dbType.db_tran_type == TABLE_XJ) {
		tr_xj = (tran_record_xj*) ptr;

		len = sizeof(tr_xj->serialnum) / sizeof(tr_xj->serialnum[0]);
		for (i = 0; i < len; i++) {
			memset(m_tmp, 0, sizeof(m_tmp));
			sprintf(m_tmp, "%c", tr_xj->serialnum[i]);
			strcat(m_serialnum, m_tmp);
		}

		len = sizeof(tr_xj->starttime) / sizeof(tr_xj->starttime[0]);
		for (i = 0; i < len; i++) {
			memset(m_tmp, 0, sizeof(m_tmp));
			sprintf(m_tmp, "%02x", tr_xj->starttime[i]);
			strcat(m_starttime, m_tmp);
		}

		len = sizeof(tr_xj->endtime) / sizeof(tr_xj->endtime[0]);
		for (i = 0; i < len; i++) {
			memset(m_tmp, 0, sizeof(m_tmp));
			sprintf(m_tmp, "%02x", tr_xj->endtime[i]);
			strcat(m_endtime, m_tmp);
		}

		len = sizeof(tr_xj->userId) / sizeof(tr_xj->userId[0]);
		for (i = 0; i < len; i++) {
			memset(m_tmp, 0, sizeof(m_tmp));
			sprintf(m_tmp, "%02x", tr_xj->userId[i]);
			strcat(m_userId, m_tmp);
		}

		len = sizeof(tr_xj->cardId) / sizeof(tr_xj->cardId[0]);
		for (i = 0; i < len; i++) {
			memset(m_tmp, 0, sizeof(m_tmp));
			sprintf(m_tmp, "%02x", tr_xj->cardId[i]);
			strcat(m_cardId, m_tmp);
		}

		len = sizeof(tr_xj->carlogo) / sizeof(tr_xj->carlogo[0]);
		for (i = 0; i < len; i++) {
			memset(m_tmp, 0, sizeof(m_tmp));
			sprintf(m_tmp, "%02x", tr_xj->carlogo[i]);
			strcat(m_carlogo, m_tmp);
		}

		sprintf(SQL_cmd,
				"insert into %s([guntab],[flag_finished],[serialnum],[starttime],[endtime],[charge_duration],"
				"[start_soc],[end_soc],[userId],[cardId],[stop_reason],[w_start],[w_end],"
				"[w_0],[w_1],[w_2],[w_3],[w_4],[w_5],[w_6],[w_7],[w_8],[w_9],"
				"[w_10],[w_11],[w_12],[w_13],[w_14],[w_15],[w_16],[w_17],[w_18],[w_19],"
				"[w_20],[w_21],[w_22],[w_23],[w_24],[w_25],[w_26],[w_27],[w_28],[w_29],"
				"[w_30],[w_31],[w_32],[w_33],[w_34],[w_35],[w_36],[w_37],[w_38],[w_39],"
				"[w_40],[w_41],[w_42],[w_43],[w_44],[w_45],[w_46],[w_47],[w_48],"
				"[cost_elec],[cost_server],[carlogo],[billmode],[chargevalue],[startmode],[g_stop_detail])  "
				"values(%d, %d, \"%s\", \"%s\", \"%s\", %d,"
				"%d,%d, \"%s\", \"%s\",%d,%d,%d,"
				"%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,"
				"%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,"
				"%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,"
				"%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,"
				"%d,%d,%d,%d,%d,%d,%d,%d,%d,"
				"%d,%d,\"%s\", %d,%d,%d,%d);", TRAN_RECORD_TABLE_XJ,
				tr_xj->guntab, tr_xj->flag_finished, m_serialnum, m_starttime,
				m_endtime, tr_xj->charge_duration, tr_xj->start_soc,
				tr_xj->end_soc, m_userId, m_cardId, tr_xj->stop_reason,
				tr_xj->w_start, tr_xj->w_end, tr_xj->w[0], tr_xj->w[1],
				tr_xj->w[2], tr_xj->w[3], tr_xj->w[4], tr_xj->w[5], tr_xj->w[6],
				tr_xj->w[7], tr_xj->w[8], tr_xj->w[9], tr_xj->w[10],
				tr_xj->w[11], tr_xj->w[12], tr_xj->w[13], tr_xj->w[14],
				tr_xj->w[15], tr_xj->w[16], tr_xj->w[17], tr_xj->w[18],
				tr_xj->w[19], tr_xj->w[20], tr_xj->w[21], tr_xj->w[22],
				tr_xj->w[23], tr_xj->w[24], tr_xj->w[25], tr_xj->w[26],
				tr_xj->w[27], tr_xj->w[28], tr_xj->w[29], tr_xj->w[30],
				tr_xj->w[31], tr_xj->w[32], tr_xj->w[33], tr_xj->w[34],
				tr_xj->w[35], tr_xj->w[36], tr_xj->w[37], tr_xj->w[38],
				tr_xj->w[39], tr_xj->w[40], tr_xj->w[41], tr_xj->w[42],
				tr_xj->w[43], tr_xj->w[44], tr_xj->w[45], tr_xj->w[46],
				tr_xj->w[47], tr_xj->w[48], tr_xj->cost_elec,
				tr_xj->cost_server, m_carlogo, tr_xj->billmode,
				tr_xj->chargevalue, tr_xj->startmode, tr_xj->g_stop_detail);
	}

#ifdef DEBUG
	printf("SQL_cmd :%s\n", SQL_cmd);
#endif

	if (sqlite3_exec(tr_db, SQL_cmd, 0, 0, &pErrMsg) != SQLITE_OK) {
		printf("##libdatabase## insert tcuaccount.db fail-->%s\n", pErrMsg);
		sqlite3_free(pErrMsg);
		sqlite3_close(tr_db);
		tr_db = NULL;
		return ERROR_FAIL;
	}

	printf("##libdatabase## insert tcuaccount.db success! serialnum:%s\n",
			m_serialnum);
	sqlite3_free(pErrMsg);
	return ERROR_OK;
}

//更新交易记录
static int update_tran_record(void* ptr) {
	char *pErrMsg = NULL;
	char SQL_cmd[1024] = { 0 };
	char m_serialnum[65] = { 0 };
	char m_endtime[15] = { 0 };
	char m_tmp[3] = { 0 };
	int i = 0, len = 0;
	tran_record* tr_nw = NULL;
	tran_record_xj* tr_xj = NULL;

	if (!ptr) {
		printf("##libdatabase## %s fail, due to para is NULL!!!\n", __func__);
		return ERROR_INPARA;
	}

	if (dbType.db_tran_type == TABLE_NW || dbType.db_tran_type == TABLE_YCT) {
		tr_nw = (tran_record*) ptr;
		len = sizeof(tr_nw->serialnum) / sizeof(tr_nw->serialnum[0]);
		for (i = 0; i < len; i++) {
			memset(m_tmp, 0, sizeof(m_tmp));
			sprintf(m_tmp, "%02x", tr_nw->serialnum[i]);
			strcat(m_serialnum, m_tmp);
		}

		len = sizeof(tr_nw->endtime) / sizeof(tr_nw->endtime[0]);
		for (i = 0; i < len; i++) {
			memset(m_tmp, 0, sizeof(m_tmp));
			sprintf(m_tmp, "%02x", tr_nw->endtime[i]);
			strcat(m_endtime, m_tmp);
		}

		//由于数据库周期性更新，所以只对阶段变化的值进行更新，在启动充电前能够获得的数据不在进行更新
		if (dbType.db_tran_type == TABLE_NW) {
			sprintf(SQL_cmd,
					"update %s set flag_finished=%d,endtime=\'%s\',charge_duration=%d,stop_reason=%d,"
					"billbytm=%d,w_start_0=%d,w_start_1=%d,w_start_2=%d,w_start_3=%d,w_start_4=%d,w_end_0=%d,w_end_1=%d,"
					"w_end_2=%d,w_end_3=%d,w_end_4=%d,w_0=%d,w_1=%d,w_2=%d,w_3=%d,w_4=%d,cost_elec_0=%d,cost_elec_1=%d,"
					"cost_elec_2=%d,cost_elec_3=%d,cost_elec_4=%d,cost_server=%d,cost_Parking=%d,rateprice_0=%d,rateprice_1=%d,"
					"rateprice_2=%d,rateprice_3=%d,rateprice_4=%d,rateprice_5=%d,rateprice_6=%d where serialnum=\"%s\"; ",
					TRAN_RECORD_TABLE_NW, tr_nw->flag_finished, m_endtime,
					tr_nw->charge_duration, tr_nw->stop_reason, tr_nw->billbytm,
					tr_nw->w_start[0], tr_nw->w_start[1], tr_nw->w_start[2],
					tr_nw->w_start[3], tr_nw->w_start[4], tr_nw->w_end[0],
					tr_nw->w_end[1], tr_nw->w_end[2], tr_nw->w_end[3],
					tr_nw->w_end[4], tr_nw->w[0], tr_nw->w[1], tr_nw->w[2],
					tr_nw->w[3], tr_nw->w[4], tr_nw->cost_elec[0],
					tr_nw->cost_elec[1], tr_nw->cost_elec[2],
					tr_nw->cost_elec[3], tr_nw->cost_elec[4],
					tr_nw->cost_server, tr_nw->cost_Parking,
					tr_nw->rateprice[0], tr_nw->rateprice[1],
					tr_nw->rateprice[2], tr_nw->rateprice[3],
					tr_nw->rateprice[4], tr_nw->rateprice[5],
					tr_nw->rateprice[6], m_serialnum);
		} else {
			char m_tac[9] = { 0 };
			len = sizeof(tr_nw->TAC) / sizeof(tr_nw->TAC[0]);
			for (i = 0; i < len; i++) {
				memset(m_tmp, 0, sizeof(m_tmp));
				sprintf(m_tmp, "%02x", tr_nw->TAC[i]);
				strcat(m_tac, m_tmp);
			}
			sprintf(SQL_cmd,
					"update %s set flag_finished=%d,endtime=\'%s\',charge_duration=%d,stop_reason=%d,"
					"billbytm=%d,w_start_0=%d,w_start_1=%d,w_start_2=%d,w_start_3=%d,w_start_4=%d,w_end_0=%d,w_end_1=%d,"
					"w_end_2=%d,w_end_3=%d,w_end_4=%d,w_0=%d,w_1=%d,w_2=%d,w_3=%d,w_4=%d,cost_elec_0=%d,cost_elec_1=%d,"
					"cost_elec_2=%d,cost_elec_3=%d,cost_elec_4=%d,cost_server=%d,cost_Parking=%d,rateprice_0=%d,rateprice_1=%d,"
					"rateprice_2=%d,rateprice_3=%d,rateprice_4=%d,rateprice_5=%d,rateprice_6=%d ,"
					"flag_card_settled=%d, tac=\'%s\',ticket_trade_count=%d,trade_money=%d, balance=%d where serialnum=\"%s\"; ",
					TRAN_RECORD_TABLE_YCT, tr_nw->flag_finished, m_endtime,
					tr_nw->charge_duration, tr_nw->stop_reason, tr_nw->billbytm,
					tr_nw->w_start[0], tr_nw->w_start[1], tr_nw->w_start[2],
					tr_nw->w_start[3], tr_nw->w_start[4], tr_nw->w_end[0],
					tr_nw->w_end[1], tr_nw->w_end[2], tr_nw->w_end[3],
					tr_nw->w_end[4], tr_nw->w[0], tr_nw->w[1], tr_nw->w[2],
					tr_nw->w[3], tr_nw->w[4], tr_nw->cost_elec[0],
					tr_nw->cost_elec[1], tr_nw->cost_elec[2],
					tr_nw->cost_elec[3], tr_nw->cost_elec[4],
					tr_nw->cost_server, tr_nw->cost_Parking,
					tr_nw->rateprice[0], tr_nw->rateprice[1],
					tr_nw->rateprice[2], tr_nw->rateprice[3],
					tr_nw->rateprice[4], tr_nw->rateprice[5],
					tr_nw->rateprice[6], tr_nw->flag_card_settled, m_tac,
					tr_nw->yct_nosettle_trade.ticket_trade_count,
					tr_nw->yct_nosettle_trade.trade_money,
					tr_nw->yct_nosettle_trade.balance, m_serialnum);
		}

	} else if (dbType.db_tran_type == TABLE_XJ) {
		tr_xj = (tran_record_xj*) ptr;

		len = sizeof(tr_xj->serialnum) / sizeof(tr_xj->serialnum[0]);
		for (i = 0; i < len; i++) {
			memset(m_tmp, 0, sizeof(m_tmp));
			sprintf(m_tmp, "%c", tr_xj->serialnum[i]);
			strcat(m_serialnum, m_tmp);
		}

		len = sizeof(tr_xj->endtime) / sizeof(tr_xj->endtime[0]);
		for (i = 0; i < len; i++) {
			memset(m_tmp, 0, sizeof(m_tmp));
			sprintf(m_tmp, "%02x", tr_xj->endtime[i]);
			strcat(m_endtime, m_tmp);
		}

		sprintf(SQL_cmd,
				"update %s set flag_finished=%d,endtime=\'%s\',charge_duration=%d, start_soc=%d , end_soc=%d, stop_reason=%d,"
				"w_start=%d, w_end=%d, w_0=%d, w_1=%d, w_2=%d, w_3=%d, w_4=%d, w_5=%d, w_6=%d, w_7=%d, w_8=%d, w_9=%d,"
				"w_10=%d, w_11=%d, w_12=%d, w_13=%d, w_14=%d, w_15=%d, w_16=%d, w_17=%d, w_18=%d, w_19=%d,"
				"w_20=%d, w_21=%d, w_22=%d, w_23=%d, w_24=%d, w_25=%d, w_26=%d, w_27=%d, w_28=%d, w_29=%d,"
				"w_30=%d, w_31=%d, w_32=%d, w_33=%d, w_34=%d, w_35=%d, w_36=%d, w_37=%d, w_38=%d, w_39=%d,"
				"w_40=%d, w_41=%d, w_42=%d, w_43=%d, w_44=%d, w_45=%d, w_46=%d, w_47=%d, w_48=%d,"
				"cost_elec=%d, cost_server=%d , billmode=%d, chargevalue=%d, startmode=%d,g_stop_detail=%d where serialnum=\"%s\"; ",
				TRAN_RECORD_TABLE_XJ, tr_xj->flag_finished, m_endtime,
				tr_xj->charge_duration, tr_xj->start_soc, tr_xj->end_soc,
				tr_xj->stop_reason, tr_xj->w_start, tr_xj->w_end, tr_xj->w[0],
				tr_xj->w[1], tr_xj->w[2], tr_xj->w[3], tr_xj->w[4], tr_xj->w[5],
				tr_xj->w[6], tr_xj->w[7], tr_xj->w[8], tr_xj->w[9],
				tr_xj->w[10], tr_xj->w[11], tr_xj->w[12], tr_xj->w[13],
				tr_xj->w[14], tr_xj->w[15], tr_xj->w[16], tr_xj->w[17],
				tr_xj->w[18], tr_xj->w[19], tr_xj->w[20], tr_xj->w[21],
				tr_xj->w[22], tr_xj->w[23], tr_xj->w[24], tr_xj->w[25],
				tr_xj->w[26], tr_xj->w[27], tr_xj->w[28], tr_xj->w[29],
				tr_xj->w[30], tr_xj->w[31], tr_xj->w[32], tr_xj->w[33],
				tr_xj->w[34], tr_xj->w[35], tr_xj->w[36], tr_xj->w[37],
				tr_xj->w[38], tr_xj->w[39], tr_xj->w[40], tr_xj->w[41],
				tr_xj->w[42], tr_xj->w[43], tr_xj->w[44], tr_xj->w[45],
				tr_xj->w[46], tr_xj->w[47], tr_xj->w[48], tr_xj->cost_elec,
				tr_xj->cost_server, tr_xj->billmode, tr_xj->chargevalue,
				tr_xj->startmode, tr_xj->g_stop_detail, m_serialnum);

	}
#ifdef DEBUG
	printf("##libdatabase## %s\n", SQL_cmd);
#endif

	if (sqlite3_exec(tr_db, SQL_cmd, 0, 0, &pErrMsg) != SQLITE_OK) {
		printf("##libdatabase## update tcuaccount.db fail-->%s\n", pErrMsg);
		sqlite3_free(pErrMsg);
		sqlite3_close(tr_db);
		tr_db = NULL;
		return ERROR_FAIL;
	}

	sqlite3_free(pErrMsg);
	printf("##libdatabase## update tcuaccount.db  success! serialnum:%s\n",
			m_serialnum);
	return ERROR_OK;
}

//数据库注册，标注各表的类型
int protocol_database_register(db_register* data) {
	if (data == NULL) {
		printf(
				"##libdatabase## Databse Register, due to para is NULL, adopt default dbType!\n");
		return 0;
	}

	if (data->db_tran_type != TABLE_NW && data->db_tran_type != TABLE_XJ
			&& data->db_tran_type != TABLE_YCT) {
		dbType.db_tran_type = TABLE_NW;
		return 1;
	} else
		dbType.db_tran_type = data->db_tran_type;

	if (data->db_err_type != TABLE_NW && data->db_err_type != TABLE_XJ && data->db_err_type != TABLE_YCT) {
		dbType.db_err_type = TABLE_NW;
		return 1;
	} else
		dbType.db_err_type = data->db_err_type;

	if (data->db_card_type != TABLE_NW && data->db_card_type != TABLE_XJ && data->db_card_type != TABLE_YCT) {
		dbType.db_card_type = TABLE_NW;
		return 1;
	} else
		dbType.db_card_type = data->db_card_type;

	printf("##libdatabases## registe database success, dbType.db_tran_type=%d ,dbType.db_err_type=%d "
			" dbType.db_card_type=%d\n",dbType.db_tran_type, dbType.db_err_type,dbType.db_card_type);
	return 0;
}

//周期性更新交易数据库，0成功,1失败
int protocol_updateCD(void *precord) {
	char *pErrMsg = NULL;
	char SQL_cmd[256] = { 0 };
	tran_record *tr_nw = NULL;
	tran_record *tr_yct = NULL;
	tran_record_xj* tr_xj = NULL;
	int ret = 0;
	int nrow = 0, ncolnum = 0;
	char **pResult = NULL;
	char m_serialnum[65] = { 0 };
	char m_tmp[3] = { 0 };
	int i = 0, len = 0;

	if (!precord) {
		printf("##libdatabase## %s fail, due to para is NULL!!!", __func__);
		return ERROR_INPARA;
	}

	if (dbType.db_tran_type == TABLE_NW)
		tr_nw = (tran_record*) precord;
	else if (dbType.db_tran_type == TABLE_YCT)
		tr_yct = (tran_record*) precord;
	else if (dbType.db_tran_type == TABLE_XJ)
		tr_xj = (tran_record_xj*) precord;
	else {
		printf(
				"##libdatabase## %s fail, due to tran record table type is error !!!",
				__func__);
		return ERROR_INPARA;
	}

	ret = file_is_exist(TRAN_RECORD_PATH, TRAN_DB_TYPE);
	if (ret == ERROR_INPARA)
		return ERROR_FAIL;
	else if (ret == ERROR_FAIL) {
		if (creat_tran_record_db() != ERROR_OK)
			return ERROR_FAIL;
	}

	if (tr_db == NULL)
		if (sqlite3_open(TRAN_RECORD_PATH, &tr_db) != SQLITE_OK) {
			sqlite3_close(tr_db);
			tr_db = NULL;

			return ERROR_FAIL;
		}

	if (dbType.db_tran_type == TABLE_NW) {
		len = sizeof(tr_nw->serialnum) / sizeof(tr_nw->serialnum[0]);
		for (i = 0; i < len; i++) {
			memset(m_tmp, 0, sizeof(m_tmp));
			sprintf(m_tmp, "%02x", tr_nw->serialnum[i]);
			strcat(m_serialnum, m_tmp);
		}
		sprintf(SQL_cmd, "select * from %s where serialnum=\'%s\';",
				TRAN_RECORD_TABLE_NW, m_serialnum);
	}
	else if (dbType.db_tran_type == TABLE_YCT) {
		len = sizeof(tr_yct->serialnum) / sizeof(tr_yct->serialnum[0]);
		for (i = 0; i < len; i++) {
			memset(m_tmp, 0, sizeof(m_tmp));
			sprintf(m_tmp, "%02x", tr_yct->serialnum[i]);
			strcat(m_serialnum, m_tmp);
		}
		sprintf(SQL_cmd, "select * from %s where serialnum=\'%s\';",
				TRAN_RECORD_TABLE_YCT, m_serialnum);
	} else if (dbType.db_tran_type == TABLE_XJ) {
		len = sizeof(tr_xj->serialnum) / sizeof(tr_xj->serialnum[0]);
		for (i = 0; i < len; i++) {
			memset(m_tmp, 0, sizeof(m_tmp));
			sprintf(m_tmp, "%c", tr_xj->serialnum[i]);
			strcat(m_serialnum, m_tmp);
		}
		sprintf(SQL_cmd, "select * from %s where serialnum=\'%s\';",
				TRAN_RECORD_TABLE_XJ, m_serialnum);
		//		sprintf(SQL_cmd,"select * from %s where serialnum=\'%s\';",TRAN_RECORD_TABLE_XJ,tr_xj->serialnum);
	}

	if (sqlite3_get_table(tr_db, SQL_cmd, &pResult, &nrow, &ncolnum,
			&pErrMsg) != SQLITE_OK) {
		printf(
				"##libdatabase## update tcuaccount.db get table info fail!--->%s\n",
				pErrMsg);
		sqlite3_free(pErrMsg);
		//		sqlite3_free_table(pResult);
		sqlite3_close(tr_db);
		tr_db = NULL;

		return ERROR_FAIL;
	}

	if (nrow == 0)
		ret = insert_tran_record(precord);
	else if (nrow == 1) {
		//		if(tr->flag_finished == TRAN_START){
		//			sqlite3_free(pErrMsg);
		//			sqlite3_free_table(pResult);
		//			printf("##libdatabase##update tcuaccount.db fail,due to serialnum exist and tran state error!!---%s\n",m_serialnum);
		//			return ERROR_FAIL;
		//		}
		ret = update_tran_record(precord);
	} else {
		memset(SQL_cmd, 0, sizeof(SQL_cmd));
		if (dbType.db_tran_type == TABLE_NW)
			sprintf(SQL_cmd, "delete from %s where serialnum=\'%s\';",
					TRAN_RECORD_TABLE_NW, m_serialnum);
		else if (dbType.db_tran_type == TABLE_YCT)
			sprintf(SQL_cmd, "delete from %s where serialnum=\'%s\';",
					TRAN_RECORD_TABLE_YCT, m_serialnum);
		else if (dbType.db_tran_type == TABLE_XJ)
			sprintf(SQL_cmd, "delete from %s where serialnum=\'%s\';",
					TRAN_RECORD_TABLE_XJ, tr_xj->serialnum);

		printf("##libdatabase## %s\n", SQL_cmd);

		if (sqlite3_exec(tr_db, SQL_cmd, 0, 0, &pErrMsg) != SQLITE_OK) {
			sqlite3_free(pErrMsg);
			sqlite3_free_table(pResult);

			return ERROR_FAIL;
		}
		ret = insert_tran_record(precord);
		memset(SQL_cmd, 0, sizeof(SQL_cmd));
		sprintf(SQL_cmd, "select * from tagAccount where serialnum=\'%s\';",
				m_serialnum);

	}

	sqlite3_exec(tr_db, SQL_cmd, callback_func, 0, &pErrMsg);
	sqlite3_free(pErrMsg);
	sqlite3_free_table(pResult);

	protocol_database_manager(TRAN_RECORD_PATH, TRAN_DB_TYPE);

	if(tr_db != NULL){
		printf("##libdatabase## %s  close tcuaccount.db\n",__func__);
		sqlite3_close(tr_db);
		tr_db = NULL;
	}

	return ret;

}

int protocol_deleteCD(void* precord) {
	char *pErrMsg = NULL;
	char SQL_cmd[256] = { 0 };
	tran_record *tr_nw = NULL;
	tran_record_xj* tr_xj = NULL;
	char **pResult = NULL;
	char m_serialnum[65] = { 0 };
	char m_tmp[3] = { 0 };
	int ret = 0;
	int i = 0, len = 0;

	if (!precord) {
		printf("##libdatabase## %s fail, due to para is NULL!!!\n", __func__);
		return ERROR_INPARA;
	}

	if (dbType.db_tran_type == TABLE_NW || dbType.db_tran_type == TABLE_YCT)
		tr_nw = (tran_record*) precord;
	else if (dbType.db_tran_type == TABLE_XJ)
		tr_xj = (tran_record_xj*) precord;
	else {
		printf("##libdatabase## %s fail, due to dbType Unrecognizable!\n",
				__func__);
		return ERROR_FAIL;
	}

	ret = file_is_exist(TRAN_RECORD_PATH, TRAN_DB_TYPE);
	if (ret == ERROR_INPARA)
		return ERROR_FAIL;
	else if (ret == ERROR_FAIL) {
		if (creat_tran_record_db() != ERROR_OK)
			return ERROR_FAIL;
	}

	if (tr_db == NULL)
		if (sqlite3_open(TRAN_RECORD_PATH, &tr_db) != SQLITE_OK) {
			sqlite3_close(tr_db);
			tr_db = NULL;

			return ERROR_FAIL;
		}

	if (dbType.db_tran_type == TABLE_NW || dbType.db_tran_type == TABLE_YCT) {
		len = sizeof(tr_nw->serialnum) / sizeof(tr_nw->serialnum[0]);
		for (i = 0; i < len; i++) {
			memset(m_tmp, 0, sizeof(m_tmp));
			sprintf(m_tmp, "%02x", tr_nw->serialnum[i]);
			strcat(m_serialnum, m_tmp);
		}
		if (dbType.db_tran_type == TABLE_NW)
			sprintf(SQL_cmd, "delete from %s where serialnum=\'%s\';",
					TRAN_RECORD_TABLE_NW, m_serialnum);
		else
			sprintf(SQL_cmd, "delete from %s where serialnum=\'%s\';",
					TRAN_RECORD_TABLE_YCT, m_serialnum);
	} else if (dbType.db_tran_type == TABLE_XJ) {
		len = sizeof(tr_xj->serialnum) / sizeof(tr_xj->serialnum[0]);
		for (i = 0; i < len; i++) {
			memset(m_tmp, 0, sizeof(m_tmp));
			sprintf(m_tmp, "%c", tr_xj->serialnum[i]);
			strcat(m_serialnum, m_tmp);
		}
		//		sprintf(SQL_cmd,"delete from %s where serialnum=\'%s\';",TRAN_RECORD_TABLE_XJ,tr_xj->serialnum);
	}

	printf("##libdatabase## %s \n", SQL_cmd);
	if (sqlite3_exec(tr_db, SQL_cmd, 0, 0, &pErrMsg) != SQLITE_OK) {
		printf("##libdatabase## delete tcuaccount.db fail!!!--->%s\n", pErrMsg);
		sqlite3_free(pErrMsg);
		//		sqlite3_free_table(pResult);

		return ERROR_FAIL;
	}

	printf("##libdatabase## delete tcuaccount.db success! serialnum=%s\n",
			m_serialnum);

	sqlite3_free(pErrMsg);
	sqlite3_free_table(pResult);

	if(tr_db != NULL){
		printf("##libdatabase## %s  close tcuaccount.db\n",__func__);
		sqlite3_close(tr_db);
		tr_db = NULL;
	}

	return ERROR_OK;
}

static unsigned char atoh(const char ch) {
	if (ch >= '0' && ch <= '9')
		return (unsigned char) atoi(&ch);
	else if (ch >= 'A' && ch <= 'F')
		return (0xa + (ch - 'A'));
	else if (ch >= 'a' && ch <= 'f')
		return (0xa + (ch - 'a'));
	else
		printf("##libdatabase## atoh fail !!!----ch=%c\n", ch);
	return 0;
}

static int set_CD_value(int *ncolnum, int *index, char **pResult, void *ptr) {
	int i = 0;
	char m_serialnum[65] = { 0 };
	char m_endtime[15] = { 0 };
	char m_starttime[15] = { 0 };
	char m_payCard[17] = { 0 };
	char m_userId[17] = { 0 };
	char m_cardId[17] = { 0 };
	char m_modelID[17] = { 0 };
	char m_carlogo[35] = { 0 };
	tran_record_xj* tr_xj = NULL;
	tran_record* tr_nw = NULL;

	if ((!ncolnum) || (!index) || (!ptr) || (!pResult)) {
		printf("##libdatabase## %s fail, due to para is NULL!!!\n", __func__);
		return ERROR_INPARA;
	}

	if (dbType.db_tran_type == TABLE_NW || dbType.db_tran_type == TABLE_YCT) {
		tr_nw = (tran_record*) ptr;

		tr_nw->guntab = (char) atoi(pResult[(*index) + GUNTAB_OFFSET]);
		tr_nw->flag_finished = (char) atoi(
				pResult[(*index) + FLAG_FINISHED_OFFSET]);

		snprintf(m_serialnum, 33, "%s", pResult[(*index) + SERIALNUM_OFFSET]);
		for (i = 0; i < 16; i++)
			tr_nw->serialnum[i] = (atoh(m_serialnum[2 * i]) << 4
					| atoh(m_serialnum[2 * i + 1]));

		snprintf(m_starttime, 15, "%s", pResult[(*index) + STARTTIME_OFFSET]);
		for (i = 0; i < 7; i++)
			tr_nw->starttime[i] = (atoh(m_starttime[2 * i]) << 4
					| atoh(m_starttime[2 * i + 1]));

		snprintf(m_endtime, 15, "%s", pResult[(*index) + ENDTIME_OFFSET]);
		for (i = 0; i < 7; i++)
			tr_nw->endtime[i] = (atoh(m_endtime[2 * i]) << 4
					| atoh(m_endtime[2 * i + 1]));

		tr_nw->charge_duration = atoi(
				pResult[(*index) + CHARGE_DURATION_OFFSET]);

		snprintf(m_payCard, 17, "%s", pResult[(*index) + PAYCARD_OFFSET]);
		for (i = 0; i < 8; i++)
			tr_nw->payCard[i] = (atoh(m_payCard[2 * i]) << 4
					| atoh(m_payCard[2 * i + 1]));

		snprintf(m_userId, 17, "%s", pResult[(*index) + USERID_OFFSET]);
		for (i = 0; i < 8; i++)
			tr_nw->userId[i] = (atoh(m_userId[2 * i]) << 4
					| atoh(m_userId[2 * i + 1]));

		snprintf(m_cardId, 17, "%s", pResult[(*index) + CARDID_OFFSET]);
		for (i = 0; i < 8; i++)
			tr_nw->cardId[i] = (atoh(m_cardId[2 * i]) << 4
					| atoh(m_cardId[2 * i + 1]));

		snprintf(m_modelID, 17, "%s", pResult[(*index) + MODELID_OFFSET]);
		for (i = 0; i < 8; i++)
			tr_nw->modelID[i] = (atoh(m_modelID[2 * i]) << 4
					| atoh(m_modelID[2 * i + 1]));

		tr_nw->stop_reason = atoi(pResult[(*index) + STOP_REASON_OFFSET]);
		tr_nw->billbytm = atoi(pResult[(*index) + BILLBYTM_OFFSET]);
		tr_nw->w_start[0] = atoi(pResult[(*index) + W_START0_OFFSET]);
		tr_nw->w_start[1] = atoi(pResult[(*index) + W_START1_OFFSET]);
		tr_nw->w_start[2] = atoi(pResult[(*index) + W_START2_OFFSET]);
		tr_nw->w_start[3] = atoi(pResult[(*index) + W_START3_OFFSET]);
		tr_nw->w_start[4] = atoi(pResult[(*index) + W_START4_OFFSET]);
		tr_nw->w_end[0] = atoi(pResult[(*index) + W_END0_OFFSET]);
		tr_nw->w_end[1] = atoi(pResult[(*index) + W_END1_OFFSET]);
		tr_nw->w_end[2] = atoi(pResult[(*index) + W_END2_OFFSET]);
		tr_nw->w_end[3] = atoi(pResult[(*index) + W_END3_OFFSET]);
		tr_nw->w_end[4] = atoi(pResult[(*index) + W_END4_OFFSET]);
		tr_nw->w[0] = atoi(pResult[(*index) + W0_OFFSET]);
		tr_nw->w[1] = atoi(pResult[(*index) + W1_OFFSET]);
		tr_nw->w[2] = atoi(pResult[(*index) + W2_OFFSET]);
		tr_nw->w[3] = atoi(pResult[(*index) + W3_OFFSET]);
		tr_nw->w[4] = atoi(pResult[(*index) + W4_OFFSET]);
		tr_nw->cost_elec[0] = atoi(pResult[(*index) + COST_ELEC0_OFFSET]);
		tr_nw->cost_elec[1] = atoi(pResult[(*index) + COST_ELEC1_OFFSET]);
		tr_nw->cost_elec[2] = atoi(pResult[(*index) + COST_ELEC2_OFFSET]);
		tr_nw->cost_elec[3] = atoi(pResult[(*index) + COST_ELEC3_OFFSET]);
		tr_nw->cost_elec[4] = atoi(pResult[(*index) + COST_ELEC4_OFFSET]);
		tr_nw->cost_server = atoi(pResult[(*index) + COST_SERVER_OFFSET]);
		tr_nw->cost_Parking = atoi(pResult[(*index) + COST_PARKING_OFFSET]);
		tr_nw->rateprice[0] = atoi(pResult[(*index) + RATEPRICE0_OFFSET]);
		tr_nw->rateprice[1] = atoi(pResult[(*index) + RATEPRICE1_OFFSET]);
		tr_nw->rateprice[2] = atoi(pResult[(*index) + RATEPRICE2_OFFSET]);
		tr_nw->rateprice[3] = atoi(pResult[(*index) + RATEPRICE3_OFFSET]);
		tr_nw->rateprice[4] = atoi(pResult[(*index) + RATEPRICE4_OFFSET]);
		tr_nw->rateprice[5] = atoi(pResult[(*index) + RATEPRICE5_OFFSET]);
		tr_nw->rateprice[6] = atoi(pResult[(*index) + RATEPRICE6_OFFSET]);

		snprintf(m_carlogo, 35, "%s", pResult[(*index) + CARLOGO_OFFSET]);
		for (i = 0; i < 17; i++)
			tr_nw->carlogo[i] = (atoh(m_carlogo[2 * i]) << 4
					| atoh(m_carlogo[2 * i + 1]));

		if (dbType.db_tran_type == TABLE_YCT) {
			char m_tac[9] = { 0 }, m_logicNum[17] = { 0 }, m_phyNum[17] = { 0 };

			tr_nw->flag_card_settled = atoi(
					pResult[(*index) + FLAG_CARD_SETTLED_OFFSET]);

			snprintf(m_tac, 9, "%s", pResult[(*index) + TAC_OFFSET]);
			for (i = 0; i < 4; i++)
				tr_nw->TAC[i] = (atoh(m_tac[2 * i]) << 4
						| atoh(m_tac[2 * i + 1]));

			snprintf(m_logicNum, 17, "%s",
					pResult[(*index) + LOGIC_CARDNUM_OFFSET]);
			for (i = 0; i < 8; i++)
				tr_nw->yct_nosettle_trade.logic_cardNum[i] = (atoh(
						m_logicNum[2 * i]) << 4 | atoh(m_logicNum[2 * i + 1]));

			snprintf(m_phyNum, 17, "%s",
					pResult[(*index) + PHY_CARDNUM_OFFSET]);
			for (i = 0; i < 8; i++)
				tr_nw->yct_nosettle_trade.cardNum[i] = (atoh(m_phyNum[2 * i])
						<< 4 | atoh(m_phyNum[2 * i + 1]));

			tr_nw->yct_nosettle_trade.ticket_trade_count = atoi(
					pResult[(*index) + TICKET_TRADE_COUNT_OFFSET]);
			tr_nw->yct_nosettle_trade.trade_money = atoi(
					pResult[(*index) + TRADE_MONEY_OFFSET]);
			tr_nw->yct_nosettle_trade.balance = atoi(
					pResult[(*index) + BALANCE_OFFSET]);
		}
	} else if (dbType.db_tran_type == TABLE_XJ) {
		tr_xj = (tran_record_xj*) ptr;

		tr_xj->guntab = (char) atoi(pResult[(*index) + XJ_GUNTAB_OFFSET]);

		tr_xj->flag_finished = (char) atoi(
				pResult[(*index) + XJ_FLAG_FINISHED_OFFSET]);

		snprintf(m_serialnum, 33, "%s",
				pResult[(*index) + XJ_SERIALNUM_OFFSET]);
		for (i = 0; i < 32; i++) {
			//			tr_xj->serialnum[i] = (atoh(m_serialnum[2*i]) << 4 | atoh(m_serialnum[2*i+1]));
			tr_xj->serialnum[i] = m_serialnum[i];
		}
		//		snprintf((char*)tr_xj->serialnum,32,"%s",pResult[(*index)+XJ_SERIALNUM_OFFSET]);

		snprintf(m_starttime, 15, "%s",
				pResult[(*index) + XJ_STARTTIME_OFFSET]);
		for (i = 0; i < 7; i++)
			tr_xj->starttime[i] = (atoh(m_starttime[2 * i]) << 4
					| atoh(m_starttime[2 * i + 1]));

		snprintf(m_endtime, 15, "%s", pResult[(*index) + XJ_ENDTIME_OFFSET]);
		for (i = 0; i < 7; i++)
			tr_xj->endtime[i] = (atoh(m_endtime[2 * i]) << 4
					| atoh(m_endtime[2 * i + 1]));

		tr_xj->charge_duration = atoi(
				pResult[(*index) + XJ_CHARGE_DURATION_OFFSET]);
		tr_xj->start_soc = atoi(pResult[(*index) + XJ_START_SOC_OFFSET]);
		tr_xj->end_soc = atoi(pResult[(*index) + XJ_END_SOC_OFFSET]);

		snprintf(m_userId, 17, "%s", pResult[(*index) + XJ_USERID_OFFSET]);
		for (i = 0; i < 8; i++)
			tr_xj->userId[i] = (atoh(m_userId[2 * i]) << 4
					| atoh(m_userId[2 * i + 1]));

		snprintf(m_cardId, 17, "%s", pResult[(*index) + XJ_CARDID_OFFSET]);
		for (i = 0; i < 8; i++)
			tr_xj->cardId[i] = (atoh(m_cardId[2 * i]) << 4
					| atoh(m_cardId[2 * i + 1]));

		tr_xj->stop_reason = atoi(pResult[(*index) + XJ_STOP_REASON_OFFSET]);
		tr_xj->w_start = atoi(pResult[(*index) + XJ_W_START_OFFSET]);
		tr_xj->w_end = atoi(pResult[(*index) + XJ_W_END_OFFSET]);

		tr_xj->w[0] = atoi(pResult[(*index) + XJ_W0_OFFSET]);
		tr_xj->w[1] = atoi(pResult[(*index) + XJ_W1_OFFSET]);
		tr_xj->w[2] = atoi(pResult[(*index) + XJ_W2_OFFSET]);
		tr_xj->w[3] = atoi(pResult[(*index) + XJ_W3_OFFSET]);
		tr_xj->w[4] = atoi(pResult[(*index) + XJ_W4_OFFSET]);
		tr_xj->w[5] = atoi(pResult[(*index) + XJ_W5_OFFSET]);
		tr_xj->w[6] = atoi(pResult[(*index) + XJ_W6_OFFSET]);
		tr_xj->w[7] = atoi(pResult[(*index) + XJ_W7_OFFSET]);
		tr_xj->w[8] = atoi(pResult[(*index) + XJ_W8_OFFSET]);
		tr_xj->w[9] = atoi(pResult[(*index) + XJ_W9_OFFSET]);

		tr_xj->w[10] = atoi(pResult[(*index) + XJ_W10_OFFSET]);
		tr_xj->w[11] = atoi(pResult[(*index) + XJ_W11_OFFSET]);
		tr_xj->w[12] = atoi(pResult[(*index) + XJ_W12_OFFSET]);
		tr_xj->w[13] = atoi(pResult[(*index) + XJ_W13_OFFSET]);
		tr_xj->w[14] = atoi(pResult[(*index) + XJ_W14_OFFSET]);
		tr_xj->w[15] = atoi(pResult[(*index) + XJ_W15_OFFSET]);
		tr_xj->w[16] = atoi(pResult[(*index) + XJ_W16_OFFSET]);
		tr_xj->w[17] = atoi(pResult[(*index) + XJ_W17_OFFSET]);
		tr_xj->w[18] = atoi(pResult[(*index) + XJ_W18_OFFSET]);
		tr_xj->w[19] = atoi(pResult[(*index) + XJ_W19_OFFSET]);

		tr_xj->w[20] = atoi(pResult[(*index) + XJ_W20_OFFSET]);
		tr_xj->w[21] = atoi(pResult[(*index) + XJ_W21_OFFSET]);
		tr_xj->w[22] = atoi(pResult[(*index) + XJ_W22_OFFSET]);
		tr_xj->w[23] = atoi(pResult[(*index) + XJ_W23_OFFSET]);
		tr_xj->w[24] = atoi(pResult[(*index) + XJ_W24_OFFSET]);
		tr_xj->w[25] = atoi(pResult[(*index) + XJ_W25_OFFSET]);
		tr_xj->w[26] = atoi(pResult[(*index) + XJ_W26_OFFSET]);
		tr_xj->w[27] = atoi(pResult[(*index) + XJ_W27_OFFSET]);
		tr_xj->w[28] = atoi(pResult[(*index) + XJ_W28_OFFSET]);
		tr_xj->w[29] = atoi(pResult[(*index) + XJ_W29_OFFSET]);

		tr_xj->w[30] = atoi(pResult[(*index) + XJ_W30_OFFSET]);
		tr_xj->w[31] = atoi(pResult[(*index) + XJ_W31_OFFSET]);
		tr_xj->w[32] = atoi(pResult[(*index) + XJ_W32_OFFSET]);
		tr_xj->w[33] = atoi(pResult[(*index) + XJ_W33_OFFSET]);
		tr_xj->w[34] = atoi(pResult[(*index) + XJ_W34_OFFSET]);
		tr_xj->w[35] = atoi(pResult[(*index) + XJ_W35_OFFSET]);
		tr_xj->w[36] = atoi(pResult[(*index) + XJ_W36_OFFSET]);
		tr_xj->w[37] = atoi(pResult[(*index) + XJ_W37_OFFSET]);
		tr_xj->w[38] = atoi(pResult[(*index) + XJ_W38_OFFSET]);
		tr_xj->w[39] = atoi(pResult[(*index) + XJ_W39_OFFSET]);

		tr_xj->w[40] = atoi(pResult[(*index) + XJ_W40_OFFSET]);
		tr_xj->w[41] = atoi(pResult[(*index) + XJ_W41_OFFSET]);
		tr_xj->w[42] = atoi(pResult[(*index) + XJ_W42_OFFSET]);
		tr_xj->w[43] = atoi(pResult[(*index) + XJ_W43_OFFSET]);
		tr_xj->w[44] = atoi(pResult[(*index) + XJ_W44_OFFSET]);
		tr_xj->w[45] = atoi(pResult[(*index) + XJ_W45_OFFSET]);
		tr_xj->w[46] = atoi(pResult[(*index) + XJ_W46_OFFSET]);
		tr_xj->w[47] = atoi(pResult[(*index) + XJ_W47_OFFSET]);
		tr_xj->w[48] = atoi(pResult[(*index) + XJ_W48_OFFSET]);

		tr_xj->cost_elec = atoi(pResult[(*index) + XJ_COST_ELEC_OFFSET]);
		tr_xj->cost_server = atoi(pResult[(*index) + XJ_COST_SERVER_OFFSET]);

		snprintf(m_carlogo, 35, "%s", pResult[(*index) + XJ_CARLOGO_OFFSET]);
		for (i = 0; i < 17; i++)
			tr_xj->carlogo[i] = (atoh(m_carlogo[2 * i]) << 4
					| atoh(m_carlogo[2 * i + 1]));

		tr_xj->billmode = atoi(pResult[(*index) + XJ_BILLMODE_OFFSET]);
		tr_xj->chargevalue = atoi(pResult[(*index) + XJ_CHARGEVALUE_OFFSET]);
		tr_xj->startmode = atoi(pResult[(*index) + XJ_STARTMODE_OFFSET]);
		tr_xj->g_stop_detail = atoi(pResult[(*index) + XJ_GSTOPDETAIL_OFFSET]);

	}

	(*index) = (*index) + (*ncolnum);

	return ERROR_OK;
}

//查询充电记录
int protocol_queryCD(void* precord, int tr_num) {
	tran_record* tr_nw = NULL;
	tran_record_xj* tr_xj = NULL;
	char *pErrMsg = NULL;
	char SQL_cmd[256] = { 0 };
	int nrow = 0, ncolnum = 0;
	char **pResult = NULL;
	int i = 0, index = 0;
	int ret = 0;

	if ((!precord) || (tr_num < 0)) {
		printf(
				"##libdatabase## %s fail, due to para is NULL or is Zero num=%d\n",
				__func__, tr_num);
		return ERROR_INPARA;
	}

	if (dbType.db_tran_type == TABLE_NW) {
		tr_nw = (tran_record*) precord;
		sprintf(SQL_cmd, "select * from %s;", TRAN_RECORD_TABLE_NW);
	} else if (dbType.db_tran_type == TABLE_YCT) {
		tr_nw = (tran_record*) precord;
		sprintf(SQL_cmd, "select * from %s;", TRAN_RECORD_TABLE_YCT);
	} else if (dbType.db_tran_type == TABLE_XJ) {
		tr_xj = (tran_record_xj*) precord;
		sprintf(SQL_cmd, "select * from %s;", TRAN_RECORD_TABLE_XJ);
	} else {
		printf("##libdatabase## %s fail, due to dbType Unrecognizable!\n",
				__func__);
		return ERROR_FAIL;
	}

	if (file_is_exist(TRAN_RECORD_PATH, TRAN_DB_TYPE) != ERROR_OK) {
		if (creat_tran_record_db() != ERROR_OK)
			return ERROR_FAIL;
	}

	if (tr_db == NULL) {
		if (sqlite3_open(TRAN_RECORD_PATH, &tr_db) != SQLITE_OK) {
			sqlite3_close(tr_db);
			tr_db = NULL;
			return ERROR_FAIL;
		}
	}

	if (sqlite3_get_table(tr_db, SQL_cmd, &pResult, &nrow, &ncolnum,
			&pErrMsg) != SQLITE_OK) {
		printf(
				"##libdatabase## query tcuaccount.db get table info fail!--->%s\n",
				pErrMsg);
		sqlite3_free(pErrMsg);
		sqlite3_free_table(pResult);
		sqlite3_close(tr_db);
		tr_db = NULL;

		return ERROR_FAIL;
	}

	if (nrow == 0) {
		sqlite3_free(pErrMsg);
		sqlite3_free_table(pResult);
		return nrow;
	}

	if (nrow > 0 && nrow <= tr_num) {
		index = ncolnum;
		if (dbType.db_tran_type == TABLE_NW
				|| dbType.db_tran_type == TABLE_YCT) {
			for (i = 0; i < nrow; i++) {
				ret = set_CD_value(&ncolnum, &index, pResult, &tr_nw[i]);
				if (ret != ERROR_OK && ret != ERROR_INPARA) {
					sqlite3_free(pErrMsg);
					sqlite3_free_table(pResult);
					return ERROR_FAIL;
				}
			}
		} else if (dbType.db_tran_type == TABLE_XJ) {
			for (i = 0; i < nrow; i++) {
				ret = set_CD_value(&ncolnum, &index, pResult, &tr_xj[i]);
				if (ret != ERROR_OK && ret != ERROR_INPARA) {
					sqlite3_free(pErrMsg);
					sqlite3_free_table(pResult);
					return ERROR_FAIL;
				}
			}
		}

	} else {
		if (dbType.db_tran_type == TABLE_NW)
			sprintf(SQL_cmd,
					"select * from (select * from %s order by serialnum desc) limit %d;",
					TRAN_RECORD_TABLE_NW, tr_num);
		else if (dbType.db_tran_type == TABLE_YCT)
			sprintf(SQL_cmd,
					"select * from (select * from %s order by serialnum desc) limit %d;",
					TRAN_RECORD_TABLE_YCT, tr_num);
		else if (dbType.db_tran_type == TABLE_XJ)
			sprintf(SQL_cmd,
					"select * from (select * from %s order by serialnum desc) limit %d;",
					TRAN_RECORD_TABLE_XJ, tr_num);

		if (sqlite3_get_table(tr_db, SQL_cmd, &pResult, &nrow, &ncolnum,
				&pErrMsg) != SQLITE_OK) {
			printf(
					"##libdatabase## query tcuaccount.db get table info fail!--->%s\n",
					pErrMsg);
			sqlite3_free(pErrMsg);
			//			sqlite3_free_table(pResult);
			sqlite3_close(tr_db);
			tr_db = NULL;

			return ERROR_FAIL;
		}

		index = ncolnum;
		if (dbType.db_tran_type == TABLE_NW
				|| dbType.db_tran_type == TABLE_YCT) {
			for (i = 0; i < nrow; i++) {
				ret = set_CD_value(&ncolnum, &index, pResult, &tr_nw[i]);
				if (ret != ERROR_OK && ret != ERROR_INPARA) {
					sqlite3_free(pErrMsg);
					sqlite3_free_table(pResult);
					return ERROR_FAIL;
				}
			}
		} else if (dbType.db_tran_type == TABLE_XJ) {
			for (i = 0; i < nrow; i++) {
				ret = set_CD_value(&ncolnum, &index, pResult, &tr_xj[i]);
				if (ret != ERROR_OK && ret != ERROR_INPARA) {
					sqlite3_free(pErrMsg);
					sqlite3_free_table(pResult);
					return ERROR_FAIL;
				}
			}
		}

	}

	sqlite3_free(pErrMsg);
	sqlite3_free_table(pResult);
	printf("##libdatabase## query tcuaccount.db success! nrow=%d\n", nrow);

	if(tr_db != NULL){
		printf("##libdatabase## %s  close tcuaccount.db\n",__func__);
		sqlite3_close(tr_db);
		tr_db = NULL;
	}

	return nrow;
}

int protocol_get_offlineCD_byIndex(void* precord, int index) {
	tran_record* tr_nw = NULL;
	tran_record_xj* tr_xj = NULL;
	char *pErrMsg = NULL;
	char SQL_cmd[256] = { 0 };
	int nrow = 0, ncolnum = 0;
	char **pResult = NULL;
	int m_index = 0;
	int ret = 0;

	if ((!precord) || index < 1) {
		printf(
				"##libdatabase## %s fail, due to para is NULL or index < 1, index=%d\n",
				__func__, index);
		return ERROR_INPARA;
	}

	if (dbType.db_tran_type == TABLE_NW || dbType.db_tran_type == TABLE_YCT)
		tr_nw = (tran_record*) precord;
	else if (dbType.db_tran_type == TABLE_XJ)
		tr_xj = (tran_record_xj*) precord;
	else {
		printf(
				"##libdatabase## %s fail, due to tran database type unrecognize!",
				__func__);
		return ERROR_FAIL;
	}

	if (file_is_exist(TRAN_RECORD_PATH, TRAN_DB_TYPE) != ERROR_OK) {
		if (creat_tran_record_db() != ERROR_OK)
			return ERROR_FAIL;
	}

	if (tr_db == NULL) {
		if (sqlite3_open(TRAN_RECORD_PATH, &tr_db) != SQLITE_OK) {
			sqlite3_close(tr_db);
			tr_db = NULL;
			return ERROR_FAIL;
		}
	}

	if (dbType.db_tran_type == TABLE_NW)
		sprintf(SQL_cmd, "select * from %s where flag_finished= %d;",
				TRAN_RECORD_TABLE_NW, TRAN_OFFLINE);
	else if (dbType.db_tran_type == TABLE_YCT)
		sprintf(SQL_cmd, "select * from %s where flag_finished= %d;",
				TRAN_RECORD_TABLE_YCT, TRAN_OFFLINE);
	else if (dbType.db_tran_type == TABLE_XJ)
		sprintf(SQL_cmd, "select * from %s where flag_finished= %d;",
				TRAN_RECORD_TABLE_XJ, TRAN_OFFLINE);

	if (sqlite3_get_table(tr_db, SQL_cmd, &pResult, &nrow, &ncolnum,
			&pErrMsg) != SQLITE_OK) {
		printf(
				"##libdatabase## protocol_get_offlineCD_byIndex tcuaccount.db get table info fail!--->%s\n",
				pErrMsg);
		sqlite3_free(pErrMsg);
		//		sqlite3_free_table(pResult);
		sqlite3_close(tr_db);
		tr_db = NULL;

		return ERROR_FAIL;
	}

	if (nrow == 0) {
		sqlite3_free(pErrMsg);
		sqlite3_free_table(pResult);
		return ERROR_FAIL;
	}

	if (nrow < index) {
		sqlite3_free(pErrMsg);
		sqlite3_free_table(pResult);
		printf("##libdatabase## get offline record fail, due to nrow<index\n");
		return ERROR_FAIL;
	}

	//	(*index) = (*index) + (*ncolnum);

	m_index = index * ncolnum;
	if (dbType.db_tran_type == TABLE_NW || dbType.db_tran_type == TABLE_YCT)
		ret = set_CD_value(&ncolnum, &m_index, pResult, tr_nw);
	else if (dbType.db_tran_type == TABLE_XJ)
		ret = set_CD_value(&ncolnum, &m_index, pResult, tr_xj);

	if (ret != ERROR_OK && ret != ERROR_INPARA) {
		sqlite3_free(pErrMsg);
		sqlite3_free_table(pResult);
		return ERROR_FAIL;
	}

	sqlite3_free(pErrMsg);
	sqlite3_free_table(pResult);


	if(tr_db != NULL){
		printf("##libdatabase## %s  close tcuaccount.db\n",__func__);
		sqlite3_close(tr_db);
		tr_db = NULL;
	}
	return ERROR_OK;
}

int protocol_get_offlineCD(void* precord) {
	tran_record* tr_nw = NULL;
	tran_record_xj* tr_xj = NULL;
	char *pErrMsg = NULL;
	char SQL_cmd[256] = { 0 };
	int nrow = 0, ncolnum = 0;
	char **pResult = NULL;
	int index = 0;
	int ret = 0;

	if ((!precord)) {
		printf("##libdatabase## %s fail, due to para is NULL!\n", __func__);
		return ERROR_INPARA;
	}

	if (dbType.db_tran_type == TABLE_NW || dbType.db_tran_type == TABLE_YCT)
		tr_nw = (tran_record*) precord;
	else if (dbType.db_tran_type == TABLE_XJ)
		tr_xj = (tran_record_xj*) precord;
	else {
		printf(
				"##libdatabase## %s fail, due to tran database type unrecognize!",
				__func__);
		return ERROR_FAIL;
	}

	if (file_is_exist(TRAN_RECORD_PATH, TRAN_DB_TYPE) != ERROR_OK) {
		if (creat_tran_record_db() != ERROR_OK)
			return ERROR_FAIL;
	}

	if (tr_db == NULL) {
		if (sqlite3_open(TRAN_RECORD_PATH, &tr_db) != SQLITE_OK) {
			sqlite3_close(tr_db);
			tr_db = NULL;
			return ERROR_FAIL;
		}
	}

	if (dbType.db_tran_type == TABLE_NW)
		sprintf(SQL_cmd, "select * from %s  where flag_finished= %d;",
				TRAN_RECORD_TABLE_NW, TRAN_OFFLINE);
	else if (dbType.db_tran_type == TABLE_YCT)
		sprintf(SQL_cmd, "select * from %s  where flag_finished= %d;",
				TRAN_RECORD_TABLE_YCT, TRAN_OFFLINE);
	else if (dbType.db_tran_type == TABLE_XJ)
		sprintf(SQL_cmd, "select * from %s  where flag_finished= %d;",
				TRAN_RECORD_TABLE_XJ, TRAN_OFFLINE);

	if (sqlite3_get_table(tr_db, SQL_cmd, &pResult, &nrow, &ncolnum,
			&pErrMsg) != SQLITE_OK) {
		printf(
				"##libdatabase## protocol_get_offlineCD tcuaccount.db get table info fail!--->%s\n",
				pErrMsg);
		sqlite3_free(pErrMsg);
		//		sqlite3_free_table(pResult);
		sqlite3_close(tr_db);
		tr_db = NULL;

		return ERROR_FAIL;
	}

	if (nrow == 0) {
		sqlite3_free(pErrMsg);
		sqlite3_free_table(pResult);
		return ERROR_FAIL;
	}

	index = ncolnum;
	if (dbType.db_tran_type == TABLE_NW || dbType.db_tran_type == TABLE_YCT) {
		ret = set_CD_value(&ncolnum, &index, pResult, tr_nw);

	} else if (dbType.db_tran_type == TABLE_XJ)
		ret = set_CD_value(&ncolnum, &index, pResult, tr_xj);

	if (ret != ERROR_OK && ret != ERROR_INPARA) {
		sqlite3_free(pErrMsg);
		sqlite3_free_table(pResult);
		return ERROR_FAIL;
	}

	sqlite3_free(pErrMsg);
	sqlite3_free_table(pResult);


	if(tr_db != NULL){
		printf("##libdatabase## %s  close tcuaccount.db\n",__func__);
		sqlite3_close(tr_db);
		tr_db = NULL;
	}
	return ERROR_OK;
}

int protocol_getCD_bySerialnum(void* precord) {
	tran_record* tr_nw = NULL;
	tran_record_xj* tr_xj = NULL;
	char *pErrMsg = NULL;
	char SQL_cmd[256] = { 0 };
	int nrow = 0, ncolnum = 0;
	char **pResult = NULL;
	int index = 0;
	char m_serialnum[65] = { 0 }, m_tmp[3] = { 0 };
	int ret = 0, len = 0, i = 0;

	if ((!precord)) {
		printf("##libdatabase## %s fail, due to para is NULL!\n", __func__);
		return ERROR_INPARA;
	}

	if (file_is_exist(TRAN_RECORD_PATH, TRAN_DB_TYPE) != ERROR_OK) {
		if (creat_tran_record_db() != ERROR_OK)
			return ERROR_FAIL;
	}

	if (tr_db == NULL) {
		if (sqlite3_open(TRAN_RECORD_PATH, &tr_db) != SQLITE_OK) {
			sqlite3_close(tr_db);
			tr_db = NULL;
			return ERROR_FAIL;
		}
	}

	if (dbType.db_tran_type == TABLE_NW || dbType.db_tran_type == TABLE_YCT) {
		tr_nw = (tran_record*) precord;
		len = sizeof(tr_nw->serialnum) / sizeof(tr_nw->serialnum[0]);
		for (i = 0; i < len; i++) {
			memset(m_tmp, 0, sizeof(m_tmp));
			sprintf(m_tmp, "%02x", tr_nw->serialnum[i]);
			strcat(m_serialnum, m_tmp);
		}
		if (dbType.db_tran_type == TABLE_NW)
			sprintf(SQL_cmd, "select * from %s where serialnum=\'%s\';",
					TRAN_RECORD_TABLE_NW, m_serialnum);
		else
			sprintf(SQL_cmd, "select * from %s where serialnum=\'%s\';",
					TRAN_RECORD_TABLE_YCT, m_serialnum);
	} else if (dbType.db_tran_type == TABLE_XJ) {
		tr_xj = (tran_record_xj*) precord;
		len = sizeof(tr_xj->serialnum) / sizeof(tr_xj->serialnum[0]);
		for (i = 0; i < len; i++) {
			memset(m_tmp, 0, sizeof(m_tmp));
			sprintf(m_tmp, "%c", tr_xj->serialnum[i]);
			strcat(m_serialnum, m_tmp);
		}
		sprintf(SQL_cmd, "select * from %s where serialnum=\'%s\';",
				TRAN_RECORD_TABLE_XJ, m_serialnum);
		//		sprintf(SQL_cmd,"select * from %s where serialnum=\'%s\';",TRAN_RECORD_TABLE_XJ,tr_xj->serialnum);
	} else {
		printf(
				"##libdatabase## %s fail, due to tran database type unrecognize!",
				__func__);
		return ERROR_FAIL;
	}

	printf("%s\n", SQL_cmd);
	if (sqlite3_get_table(tr_db, SQL_cmd, &pResult, &nrow, &ncolnum,
			&pErrMsg) != SQLITE_OK) {
		printf(
				"##libdatabase## protocol_getCD_bySerialnum tcuaccount.db get table info fail!--->%s\n",
				pErrMsg);
		sqlite3_free(pErrMsg);
		sqlite3_free_table(pResult);
		sqlite3_close(tr_db);
		tr_db = NULL;

		return ERROR_FAIL;
	}

	if (nrow == 0) {
		sqlite3_free(pErrMsg);
		sqlite3_free_table(pResult);
		printf("##libdatabase##%s :%s no exist!\n", __func__, m_serialnum);
		return ERROR_FAIL;
	}

	index = ncolnum;
	if (dbType.db_tran_type == TABLE_NW || dbType.db_tran_type == TABLE_YCT)
		ret = set_CD_value(&ncolnum, &index, pResult, tr_nw);
	else if (dbType.db_tran_type == TABLE_XJ)
		ret = set_CD_value(&ncolnum, &index, pResult, tr_xj);

	if (ret != ERROR_OK && ret != ERROR_INPARA) {
		sqlite3_free(pErrMsg);
		sqlite3_free_table(pResult);
		return ERROR_FAIL;
	}

	sqlite3_free(pErrMsg);
	sqlite3_free_table(pResult);

	if(tr_db != NULL){
		printf("##libdatabase## %s  close tcuaccount.db\n",__func__);
		sqlite3_close(tr_db);
		tr_db = NULL;
	}
	return ERROR_OK;
}
/*
 * 根据卡号获取未完整交易记录
 * cardNum为调用者填充的卡号, precord为动态库查询成功时填充未完整交易记录
 * 返回值：0查询成功，其他值：查询不到
 * */
int protocol_get_offlineCD_byCardnum(unsigned char cardNum[8], void *precord) {
	tran_record* tr_nw = NULL;
	char *pErrMsg = NULL;
	char SQL_cmd[256] = { 0 }, m_cardNum[17] = { 0 }, m_tmp[3] = { 0 };
	int nrow = 0, ncolnum = 0;
	char **pResult = NULL;
	int index = 0;
	int ret = 0, i = 0;

	if ((!precord) || (!cardNum)) {
		printf("##libdatabase## %s fail, due to para is NULL!\n", __func__);
		return ERROR_INPARA;
	}

	if (dbType.db_tran_type == TABLE_YCT)
		tr_nw = (tran_record*) precord;
	else {
		printf(
				"##libdatabase## %s fail, due to tran database type unrecognize!  curDbType:%d\n",
				__func__, dbType.db_tran_type);
		return ERROR_FAIL;
	}

	if (file_is_exist(TRAN_RECORD_PATH, TRAN_DB_TYPE) != ERROR_OK) {
		if (creat_tran_record_db() != ERROR_OK)
			return ERROR_FAIL;
	}

	if (tr_db == NULL) {
		if (sqlite3_open(TRAN_RECORD_PATH, &tr_db) != SQLITE_OK) {
			sqlite3_close(tr_db);
			tr_db = NULL;
			return ERROR_FAIL;
		}
	}

	for (i = 0; i < 8; i++) {
		memset(m_tmp, 0, sizeof(m_tmp));
		sprintf(m_tmp, "%02x", cardNum[i]);
		strcat(m_cardNum, m_tmp);
	}

	sprintf(SQL_cmd,
			"select * from %s  where flag_card_settled= %d and cardId=\'%s\';",
			TRAN_RECORD_TABLE_YCT, 0x55, m_cardNum);
	printf("##libdatabase##%s\n", SQL_cmd);
	if (sqlite3_get_table(tr_db, SQL_cmd, &pResult, &nrow, &ncolnum,
			&pErrMsg) != SQLITE_OK) {
		printf(
				"##libdatabase## protocol_get_offlineCD tcuaccount.db get table info fail!--->%s\n",
				pErrMsg);
		sqlite3_free(pErrMsg);
		//		sqlite3_free_table(pResult);
		sqlite3_close(tr_db);
		tr_db = NULL;

		return ERROR_FAIL;
	}

	if (nrow == 0) {
		sqlite3_free(pErrMsg);
		sqlite3_free_table(pResult);
		printf("##libdatabase##%s :%s incomplete no exist!\n", __func__,
				m_cardNum);
		return ERROR_FAIL;
	}

	index = ncolnum;
	ret = set_CD_value(&ncolnum, &index, pResult, tr_nw);

	if (ret != ERROR_OK && ret != ERROR_INPARA) {
		sqlite3_free(pErrMsg);
		sqlite3_free_table(pResult);
		printf("##libdatabase## %s set_CD_value error, ret=%d\n",__func__,ret);
		return ERROR_FAIL;
	}

	sqlite3_free(pErrMsg);
	sqlite3_free_table(pResult);

	if(tr_db != NULL){
		printf("##libdatabase## %s  close tcuaccount.db\n",__func__);
		sqlite3_close(tr_db);
		tr_db = NULL;
	}

	return ERROR_OK;
}
/*================故障数据库====================*/

//创建故障记录数据库　成功返回0
static int creat_error_record_db(void) {
	DIR *dp = NULL;
	char *pErrMsg = NULL;
	const char *SQL_cmd =
			"create table if not exists tagError([guntab] INTEGER, [time] TEXT(15), [type] INTEGER);";

	if ((dp = opendir(DB_DIR_PATH)) == NULL)
		if (mkdir(DB_DIR_PATH, S_IRWXU | S_IRWXG | S_IRWXO) != 0) {
			err_db = NULL;
			closedir(dp);
			return ERROR_FAIL;
		}
	closedir(dp);

	if (access(ERROR_RECORD_PATH, F_OK) != 0) {
		if (creat(ERROR_RECORD_PATH, O_WRONLY | O_TRUNC) == -1) {
			err_db = NULL;
			return ERROR_FAIL;
		}
		chmod(ERROR_RECORD_PATH, S_IRWXU | S_IRWXG | S_IRWXO);
	}

	if (err_db == NULL) {
		if (sqlite3_open(ERROR_RECORD_PATH, &err_db) != SQLITE_OK) {
			sqlite3_close(err_db);
			err_db = NULL;
			return ERROR_FAIL;
		}
	}

	if (sqlite3_exec(err_db, SQL_cmd, 0, 0, &pErrMsg) != SQLITE_OK) {
		fprintf(stderr,
				"##libdatabase## Create tcuerror.db database fail!-->%s\n",
				pErrMsg);
		sqlite3_free(pErrMsg);
		sqlite3_close(err_db);
		err_db = NULL;
		return ERROR_FAIL;
	}

	sqlite3_free(pErrMsg);
	printf("##libdatabase## Create tcuerror.db database success!\n");

	return ERROR_OK;
}

//创建故障记录　成功0
static int protocol_creat_error_record(err_record* err) {
	char *pErrMsg = NULL;
	char SQL_cmd[128] = { 0 };
	char m_time[15] = { 0 };

	if (!err)
		return ERROR_INPARA;

	sprintf(m_time, "%02x%02x%02x%02x%02x%02x%02x", err->time[0], err->time[1],
			err->time[2], err->time[3], err->time[4], err->time[5],
			err->time[6]);

	sprintf(SQL_cmd, "insert into tagError values(%d,%s,%d)", err->guntab,
			m_time, err->type);

	if (sqlite3_exec(err_db, SQL_cmd, 0, 0, &pErrMsg) != SQLITE_OK) {
		fprintf(stderr, "##libdatabase## insert tcuerror.db fail! --->%s\n",
				pErrMsg);
		sqlite3_free(pErrMsg);
		sqlite3_close(err_db);
		err_db = NULL;

		return ERROR_FAIL;
	}

	printf("##libdatabase## insert tcuerror.db success!\n");
	sqlite3_free(pErrMsg);

	return ERROR_OK;
}

int protocol_updateERR(err_record* err) {
	int ret = 0, i = 0;

	if (!err)
		return ERROR_INPARA;

	ret = file_is_exist(ERROR_RECORD_PATH, ERROR_DB_TYPE);
	if (ret == ERROR_INPARA)
		return ERROR_FAIL;
	else if (ret == ERROR_FAIL) {
		if (creat_error_record_db() != ERROR_OK)
			return ERROR_FAIL;
	}

	if (err_db == NULL) {
		if (sqlite3_open(ERROR_RECORD_PATH, &err_db) != SQLITE_OK) {
			sqlite3_close(err_db);
			err_db = NULL;

			return ERROR_FAIL;
		}
	}

	if (protocol_creat_error_record(err) == ERROR_OK) {
		printf("err.guntab = %d\n", err->guntab);
		printf("err.time = ");
		for (i = 0; i < 7; i++)
			printf("%02x", err->time[i]);
		printf("\n");
		printf("err.type = %d\n", err->type);

		protocol_database_manager(ERROR_RECORD_PATH, ERROR_DB_TYPE);
		//		sqlite3_close(err_db);
		//		err_db = NULL;

		return ERROR_OK;
	}

	sqlite3_close(err_db);
	err_db = NULL;

	return ERROR_FAIL;
}

static int set_ERR_value(int *ncol, int *index, char **pResult, err_record *err) {
	int i = 0;
	char m_time[15] = { 0 };

	if (!ncol || !index || !pResult || !err)
		return ERROR_INPARA;
	err->guntab = (char) atoi(pResult[(*index) + ERR_GUNTAB_OFFSET]);

	snprintf(m_time, 15, "%s", pResult[(*index) + ERR_TIME_OFFSET]);
	for (i = 0; i < 7; i++)
		err->time[i] = (atoh(m_time[i * 2]) << 4 | atoh(m_time[i * 2 + 1]));

	err->type = (char) atoi(pResult[(*index) + ERR_TYPE_OFFSET]);

	(*index) = (*index) + (*ncol);
	return ERROR_OK;
}

int protocol_queryERR(err_record *err, int err_num) {
	char *pErrMsg = NULL;
	char SQL_cmd[128] = { 0 };
	int nrow = 0, ncol = 0;
	char **pResult = NULL;
	int i = 0, index = 0;

	if ((!err) || (err_num < 0))
		return ERROR_INPARA;

	if (file_is_exist(ERROR_RECORD_PATH, ERROR_DB_TYPE) != ERROR_OK) {
		if (creat_error_record_db() != ERROR_OK)
			return ERROR_FAIL;
	}

	if (err_db == NULL) {
		if (sqlite3_open(ERROR_RECORD_PATH, &err_db) != SQLITE_OK) {
			sqlite3_close(err_db);
			err_db = NULL;

			return ERROR_FAIL;
		}
	}

	sprintf(SQL_cmd, "select * from tagError order by time desc;");
	if (sqlite3_get_table(err_db, SQL_cmd, &pResult, &nrow, &ncol,
			&pErrMsg) == SQLITE_OK) {
		if (nrow == 0) {
			sqlite3_free(pErrMsg);
			sqlite3_free_table(pResult);
			return nrow;
		}

		index = ncol;
		if (nrow > 0 && nrow <= err_num) {
			for (i = 0; i < nrow; i++) {
				if (set_ERR_value(&ncol, &index, pResult, &err[i]) != ERROR_OK) {
					sqlite3_free(pErrMsg);
					sqlite3_free_table(pResult);
					return ERROR_FAIL;
				}
			}
		} else {
			sprintf(SQL_cmd,
					"select * from (select * from tagError order by time desc) limit %d;",
					err_num);
			sqlite3_get_table(err_db, SQL_cmd, &pResult, &nrow, &ncol,
					&pErrMsg);
			index = ncol;
			for (i = 0; i < nrow; i++) {
				if (set_ERR_value(&ncol, &index, pResult, &err[i]) != ERROR_OK) {
					sqlite3_free(pErrMsg);
					sqlite3_free_table(pResult);
					return ERROR_FAIL;
				}
			}
		}
	} else {
		printf("##libdatabase## query tcuerror.db get table info fail!--->%s\n",
				pErrMsg);
		sqlite3_free(pErrMsg);
		sqlite3_free_table(pResult);
		sqlite3_close(err_db);
		err_db = NULL;

		return ERROR_FAIL;
	}

	sqlite3_free(pErrMsg);
	sqlite3_free_table(pResult);
	printf("##libdatabase## query tcuerror.db success! nrow = %d\n", nrow);
	return nrow;
}

#if 0
/*==============系统设置数据库==================*/
int creat_sys_config_db(void)
{
	DIR *dp = NULL;
	char *pErrMsg = NULL;
	const char *SQL_cmd = "create table if not exists tagConfig([settag1] INTEGER, [bittag1] INTEGER, [dev_num] TEXT(17), [settag2] INTEGER, \
						   [bittag2] INTEGER, [apn] TEXT(64), [remote_ip1] TEXT(15), [port1] INTEGER, [remote_ip2] TEXT(16), [port2] INTEGER,\
						   [local_ip1] TEXT(16),[mask1] TEXT(16), [gateway1] TEXT(16), [local_ip2] TEXT(16), [mask2] TEXT(16), [gateway2] TEXT(16),\
						   [settag3] INTEGER, [rate_v] REAL, [over_v] REAL,[under_v] REAL,[over_v_regain] REAL,[under_v_regain] REAL,\
						   [rate_a] REAL , [over_a] REAL,  [under_a] REAL,[over_a_regain] REAL,[under_a_regain] REAL,[time] REAL);";

	if((dp = opendir(DB_DIR_PATH)) == NULL) {
		if(mkdir(DB_DIR_PATH,S_IRWXU|S_IRWXG|S_IRWXO) != 0) {
			config_db = NULL;
			closedir(dp);
			return ERROR_FAIL;
		}
	}
	closedir(dp);

	if(access(CONFIG_FILE_PATH,F_OK) != 0) {
		if(creat(CONFIG_FILE_PATH,O_WRONLY|O_TRUNC) == -1) {
			config_db = NULL;
			return ERROR_FAIL;
		}
		chmod(CONFIG_FILE_PATH,S_IRWXU|S_IRWXG|S_IRWXO);
	}

	if(config_db == NULL) {
		if(sqlite3_open(CONFIG_FILE_PATH,&config_db) != SQLITE_OK) {
			sqlite3_close(config_db);
			config_db = NULL;
			return ERROR_FAIL;
		}
	}

	if(sqlite3_exec(config_db,SQL_cmd,0,0,&pErrMsg) != SQLITE_OK) {
		fprintf(stderr,"##libdatabase## Create tcuconfig.db database fail-->%s\n",pErrMsg);
		sqlite3_free(pErrMsg);
		sqlite3_close(config_db);
		config_db = NULL;
		return ERROR_FAIL;
	}

	sqlite3_free(pErrMsg);
	printf("##libdatabase## Create tcuconfig.db database success!\n");

	return ERROR_OK;
}

int protocol_creat_sys_config(sysset_cmd* sys)
{
	char *pErrMsg = NULL;
	char SQL_cmd[1024] = {0};
	char m_dev_num[17] = {0};
	char m_remote_ip1[16] = {0};
	char m_remote_ip2[16] = {0};
	char m_local_ip1[16] = {0};
	char m_local_ip2[16] = {0};
	char m_mask1[16] = {0};
	char m_mask2[16] = {0};
	char m_gateway1[16] = {0};
	char m_gateway2[16] = {0};

	if(!sys)
		return ERROR_INPARA;

	sprintf(m_dev_num,"%02x%02x%02x%02x%02x%02x%02x%02x",sys->p1[0],sys->p1[1],sys->p1[2],sys->p1[3],sys->p1[4],sys->p1[5],sys->p1[6],sys->p1[7]);
	sprintf(m_remote_ip1,"%d.%d.%d.%d",sys->p3[0],sys->p3[1],sys->p3[2],sys->p3[3]);
	sprintf(m_remote_ip2,"%d.%d.%d.%d",sys->p5[0],sys->p5[1],sys->p5[2],sys->p5[3]);
	sprintf(m_local_ip1,"%d.%d.%d.%d",sys->p7[0],sys->p7[1],sys->p7[2],sys->p7[3]);
	sprintf(m_local_ip2,"%d.%d.%d.%d",sys->p10[0],sys->p10[1],sys->p10[2],sys->p10[3]);
	sprintf(m_mask1,"%d.%d.%d.%d",sys->p8[0],sys->p8[1],sys->p8[2],sys->p8[3]);
	sprintf(m_mask2,"%d.%d.%d.%d",sys->p11[0],sys->p11[1],sys->p11[2],sys->p11[3]);
	sprintf(m_gateway1,"%d.%d.%d.%d",sys->p9[0],sys->p9[1],sys->p9[2],sys->p9[3]);
	sprintf(m_gateway2,"%d.%d.%d.%d",sys->p12[0],sys->p12[1],sys->p12[2],sys->p12[3]);

	sprintf(SQL_cmd,"insert into tagConfig values(%d,%d,\"%s\",%d,%d,\"%s\",\"%s\",%d,\"%s\",%d,\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",%d,%.02f,%.02f,\
		%.02f,%.02f,%.02f,%.02f,%.02f,%.02f,%.02f,%.02f,%.02f);",sys->settag1,sys->bittag1,m_dev_num,sys->settag2,
		sys->bittag2,sys->p2,m_remote_ip1,sys->p4,m_remote_ip2,sys->p6,m_local_ip1,m_mask1,m_gateway1,m_local_ip2,
		m_mask2,m_gateway2,sys->settag3,sys->p13[0],sys->p13[1],sys->p13[2],sys->p13[3],sys->p13[4],sys->p13[5],
		sys->p13[6],sys->p13[7],sys->p13[8],sys->p13[9],sys->p13[10]);

	if(sqlite3_exec(config_db,SQL_cmd,0,0,&pErrMsg) != SQLITE_OK) {
		fprintf(stderr,"##libdatabase## insert tcuconfig.db fail! reason:%s\n",pErrMsg);
		sqlite3_free(pErrMsg);
		sqlite3_close(config_db);
		config_db = NULL;
		return ERROR_FAIL;
	}

	printf("##libdatabase## insert tcuconfig.db success! dev_num:%s\n",m_dev_num);
	sqlite3_free(pErrMsg);
	return ERROR_OK;
}

int protocol_update_sys_config(sysset_cmd* sys)
{
	char *pErrMsg = NULL;
	char SQL_cmd[512] = {0};
	char m_dev_num[17] = {0};
	char m_remote_ip1[16] = {0};
	char m_remote_ip2[16] = {0};
	char m_local_ip1[16] = {0};
	char m_local_ip2[16] = {0};
	char m_mask1[16] = {0};
	char m_mask2[16] = {0};
	char m_gateway1[16] = {0};
	char m_gateway2[16] = {0};

	if(!sys)
		return ERROR_INPARA;

	sprintf(m_dev_num,"%02x%02x%02x%02x%02x%02x%02x%02x",sys->p1[0],sys->p1[1],sys->p1[2],sys->p1[3],sys->p1[4],sys->p1[5],sys->p1[6],sys->p1[7]);
	sprintf(m_remote_ip1,"%d.%d.%d.%d",sys->p3[0],sys->p3[1],sys->p3[2],sys->p3[3]);
	sprintf(m_remote_ip2,"%d.%d.%d.%d",sys->p5[0],sys->p5[1],sys->p5[2],sys->p5[3]);
	sprintf(m_local_ip1,"%d.%d.%d.%d",sys->p7[0],sys->p7[1],sys->p7[2],sys->p7[3]);
	sprintf(m_local_ip2,"%d.%d.%d.%d",sys->p10[0],sys->p10[1],sys->p10[2],sys->p10[3]);
	sprintf(m_mask1,"%d.%d.%d.%d",sys->p8[0],sys->p8[1],sys->p8[2],sys->p8[3]);
	sprintf(m_mask2,"%d.%d.%d.%d",sys->p11[0],sys->p11[1],sys->p11[2],sys->p11[3]);
	sprintf(m_gateway1,"%d.%d.%d.%d",sys->p9[0],sys->p9[1],sys->p9[2],sys->p9[3]);
	sprintf(m_gateway2,"%d.%d.%d.%d",sys->p12[0],sys->p12[1],sys->p12[2],sys->p12[3]);

	sprintf(SQL_cmd,"update tagConfig set settag1=%d, bittag1=%d, dev_num=\"%s\", settag2=%d, bittag2=%d, apn=\"%s\", remote_ip1=\"%s\",port1=%d,remote_ip2=\"%s\",\
			port2=%d, local_ip1=\"%s\", mask1=\"%s\",gateway1=\"%s\", local_ip2=\"%s\",mask2=\"%s\",gateway2=\"%s\", settag3=%d, rate_v=%.02f,over_v=%.02f,\
			under_v=%.02f,over_v_regain=%.02f,under_a_regain=%.02f, rate_a=%.02f,over_a=%.02f,under_a=%.02f,over_a_regain=%.02f,under_a_regain=%.02f,\
			time=%.02f;",sys->settag1,sys->bittag1,m_dev_num,sys->settag2,
			sys->bittag2,sys->p2,m_remote_ip1,sys->p4,m_remote_ip2,sys->p6,m_local_ip1,m_mask1,m_gateway1,m_local_ip2,
			m_mask2,m_gateway2,sys->settag3,sys->p13[0],sys->p13[1],sys->p13[2],sys->p13[3],sys->p13[4],sys->p13[5],
			sys->p13[6],sys->p13[7],sys->p13[8],sys->p13[9],sys->p13[10]);

	if(sqlite3_exec(config_db,SQL_cmd,0,0,&pErrMsg) != SQLITE_OK) {
		fprintf(stderr,"##libdatabase## update tcuconfig.db fail! %s\n",pErrMsg);
		sqlite3_free(pErrMsg);
		sqlite3_close(config_db);
		config_db = NULL;
		return ERROR_FAIL;
	}

	printf("##libdatabase## update tcuconfig.db success!\n");
	sqlite3_free(pErrMsg);
	return ERROR_OK;
}

int protocol_updateSYSCONF(sysset_cmd* sys)
{
	int ret = 0;
	char *pErrMsg = NULL;
	char SQL_cmd[64] = {0};
	int nrow = 0, ncol = 0;
	char **pResult = NULL;

	if(!sys)
		return ERROR_INPARA;

	ret = file_is_exist(CONFIG_FILE_PATH,CONFIG_DB_TYPE);
	if(ret == ERROR_INPARA)
		return ERROR_FAIL;
	else if(ret == ERROR_FAIL) {
		if(creat_sys_config_db() != ERROR_OK)
			return ERROR_FAIL;
	}

	if(config_db == NULL) {
		if(sqlite3_open(CONFIG_FILE_PATH,&config_db) != SQLITE_OK) {
			sqlite3_close(config_db);
			config_db = NULL;

			return ERROR_FAIL;
		}
	}

	sprintf(SQL_cmd,"select * from tagConfig;");
	if(sqlite3_get_table(config_db,SQL_cmd,&pResult,&nrow,&ncol,&pErrMsg) != SQLITE_OK) {
		printf("##libdatabase## update tcuconfig get table info fail!--->%s\n",pErrMsg);
		sqlite3_free(pErrMsg);
		sqlite3_free_table(pResult);
		sqlite3_close(config_db);
		config_db = NULL;

		return ERROR_FAIL;
	}

	if(nrow == 0)
		ret = protocol_creat_sys_config(sys);
	else if(nrow == 1)
		ret = protocol_update_sys_config(sys);
	else {
		memset(SQL_cmd,0,sizeof(SQL_cmd));
		sprintf(SQL_cmd,"delete from tagConfig;");
		if(sqlite3_exec(config_db,SQL_cmd,0,0,&pErrMsg) != SQLITE_OK) {
			fprintf(stderr,"##libdatabase## %s fail-->%s!\n",SQL_cmd,pErrMsg);
			sqlite3_free(pErrMsg);
			sqlite3_free_table(pResult);

			return ERROR_FAIL;
		}

		ret = protocol_creat_sys_config(sys);
		memset(SQL_cmd,0,sizeof(SQL_cmd));
		sprintf(SQL_cmd,"select * from tagConfig;");
	}
	sqlite3_exec(config_db,SQL_cmd,callback_func,0,&pErrMsg);

	sqlite3_free(pErrMsg);
	sqlite3_free_table(pResult);

	return ret;
}

int set_SYSCONF_value(int *ncol, int *index, char **pResult, sysset_cmd* sys)
{
	int i = 0;
	char m_dev_num[17] = {0};

	if(!ncol || !index || !pResult || !sys)
		return ERROR_INPARA;

	sys->settag1 = (char)atoi(pResult[(*index) + SETTAG1_OFFSET]);
	sys->bittag1 = (char)atoi(pResult[(*index) + BITTAG1_OFFSET]);

	sprintf(m_dev_num,pResult[(*index) + DEV_NUM_OFFSET]);
	for(i=0; i<8; i++)
		sys->p1[i] = (atoh(m_dev_num[2*i]) << 4 | atoh(m_dev_num[2*i+1]));

	sys->settag2 = (char)atoi(pResult[(*index) + SETTAG2_OFFSET]);
	sys->bittag2 = (char)atoi(pResult[*index] + BITTAG2_OFFSET);

	sprintf(sys->p2,pResult[(*index) + APN_OFFSET]);

	sscanf(pResult[(*index) + REMOTE_IP1_OFFSET],"%hhd.%hhd.%hhd.%hhd",&sys->p3[0],&sys->p3[1],&sys->p3[2],&sys->p3[3]);
	sys->p4 =(unsigned int)atoi(pResult[(*index) + PORT1_OFFSET]);
	sscanf(pResult[(*index) + REMOTE_IP2_OFFSET],"%hhd.%hhd.%hhd.%hhd",&sys->p5[0],&sys->p5[1],&sys->p5[2],&sys->p5[3]);
	sys->p6 =(unsigned int)atoi(pResult[(*index) + PORT2_OFFSET]);
	sscanf(pResult[(*index) + LOCAL_IP1_OFFSET],"%hhd.%hhd.%hhd.%hhd",&sys->p7[0],&sys->p7[1],&sys->p7[2],&sys->p7[3]);
	sscanf(pResult[(*index) + MASK1_OFFSET],"%hhd.%hhd.%hhd.%hhd",&sys->p8[0],&sys->p8[1],&sys->p8[2],&sys->p8[3]);
	sscanf(pResult[(*index) + GATEWAY1_OFFSET],"%hhd.%hhd.%hhd.%hhd",&sys->p9[0],&sys->p9[1],&sys->p9[2],&sys->p9[3]);
	sscanf(pResult[(*index) + LOCAL_IP2_OFFSET],"%hhd.%hhd.%hhd.%hhd",&sys->p10[0],&sys->p10[1],&sys->p10[2],&sys->p10[3]);
	sscanf(pResult[(*index) + MASK2_OFFSET],"%hhd.%hhd.%hhd.%hhd",&sys->p11[0],&sys->p11[1],&sys->p11[2],&sys->p11[3]);
	sscanf(pResult[(*index) + GATEWAY2_OFFSET],"%hhd.%hhd.%hhd.%hhdd",&sys->p12[0],&sys->p12[1],&sys->p12[2],&sys->p12[3]);
	sys->settag3 = (char)atoi(pResult[(*index) + SETTAG3_OFFSET]);
	sys->p13[0] = atof(pResult[(*index) + RATE_V_OFFSET]);
	sys->p13[1] = atof(pResult[(*index) + OVER_V_OFFSET]);
	sys->p13[2] = atof(pResult[(*index) + UNDER_V_OFFSET]);
	sys->p13[3] = atof(pResult[(*index) + OVER_V_REAGIN_OFFSET]);
	sys->p13[4] = atof(pResult[(*index) + UNDER_V_REAGIN_OFFSET]);
	sys->p13[5] = atof(pResult[(*index) + RATE_A_OFFSET]);
	sys->p13[6] = atof(pResult[(*index) + OVER_A_OFFSET]);
	sys->p13[7] = atof(pResult[(*index) + UNDER_A_OFFSET]);
	sys->p13[8] = atof(pResult[(*index) + OVER_A_REAGIN_OFFSET]);
	sys->p13[9] = atof(pResult[(*index) + UNDER_A_REAGIN_OFFSET]);
	sys->p13[10] = atof(pResult[(*index) + CON_TIME_OFFSET]);

	return ERROR_OK;
}

int protocol_querySYSCONF(sysset_cmd* sys)
{
	char *pErrMsg = NULL;
	char SQL_cmd[64] = {0};
	int nrow = 0, ncol = 0;
	char **pResult = NULL;
	int index = 0;

	if(!sys)
		return ERROR_INPARA;

	if(file_is_exist(CONFIG_FILE_PATH,CONFIG_DB_TYPE) != ERROR_OK)
		if(creat_sys_config_db() != ERROR_OK)
			return ERROR_FAIL;

	if(config_db == NULL) {
		if(sqlite3_open(CONFIG_FILE_PATH,&config_db) != SQLITE_OK) {
			sqlite3_close(config_db);
			config_db = NULL;

			return ERROR_FAIL;
		}
	}

	sprintf(SQL_cmd,"select * from tagConfig;");
	if(sqlite3_get_table(config_db,SQL_cmd,&pResult,&nrow,&ncol,&pErrMsg) == SQLITE_OK) {
		if(nrow == 0)
			return ERROR_FAIL;

		index = ncol;
		if(set_SYSCONF_value(&ncol, &index, pResult, sys) != ERROR_OK) {
			sqlite3_free(pErrMsg);
			sqlite3_free_table(pResult);
			return ERROR_FAIL;
		}
	}
	else {
		printf("##libdatabase## query tcuconfig.db get table info fail!--->%s\n",pErrMsg);
		sqlite3_free(pErrMsg);
		sqlite3_free_table(pResult);
		sqlite3_close(config_db);
		config_db = NULL;

		return ERROR_FAIL;
	}

	sqlite3_free(pErrMsg);
	sqlite3_free_table(pResult);
	printf("##libdatabase## query tcuconfig.db success!\n");
	return ERROR_OK;

}

/*=================计费模型===================*/
//创建计费模型数据库
int creat_price_mode_db(void)
{
	DIR *dp = NULL;
	char *pErrMsg = NULL;
	const char* SQL_cmd = "create table if not exists tagPrice([guntab] INTEGER, [modelID] TEXT(17),[starttime] TEXT(15), [endtime] TEXT(15), [opstate] INTEGER,\
						   [metertype] INTEGER, [num] INTEGER, [time_0] INTEGER, [tag_0] INTEGER, [time_1] INTEGER,[tag_1] INTEGER, [time_2] INTEGER,\
						   [tag_2] INTEGER, [time_3] INTEGER, [tag_3] INTEGER, [time_4] INTEGER, [tag_4] INTEGER, [time_5] INTEGER, [tag_5] INTEGER,\
						   [time_6] INTEGER, [tag_6] INTEGER, [time_7] INTEGER, [tag_7] INTEGER, [time_8] INTEGER, [tag_8] INTEGER, [time_9] INTEGER,\
						   [tag_9] INTEGER, [time_10] INTEGER, [tag_10] INTEGER,[time_11] INTEGER, [tag_11] INTEGER, [rateprice_0] INTEGER, [rateprice_1] INTEGER,\
						   [rateprice_2] INTEGER, [rateprice_3] INTEGER, [rateprice_4] INTEGER, [rateprice_5] INTEGER, [rateprice_6] INTEGER);";

	if((dp = opendir(DB_DIR_PATH)) == NULL)
		if(mkdir(DB_DIR_PATH,S_IRWXU|S_IRWXG|S_IRWXO) != 0) {
			price_db = NULL;
			closedir(dp);
			return ERROR_FAIL;
		}
	closedir(dp);

	if(access(PRICE_MODE_PATH,F_OK) != 0) {
		if(creat(PRICE_MODE_PATH,O_WRONLY|O_TRUNC) == -1) {
			price_db = NULL;
			return ERROR_FAIL;
		}
		chmod(PRICE_MODE_PATH,S_IRWXU|S_IRWXG|S_IRWXO);
	}

	if(price_db == NULL)
		if(sqlite3_open(PRICE_MODE_PATH,&price_db) != SQLITE_OK) {
			sqlite3_close(price_db);
			price_db = NULL;

			return ERROR_FAIL;
		}

	if(sqlite3_exec(price_db,SQL_cmd,0,0,&pErrMsg) != SQLITE_OK) {
		fprintf(stderr,"##libdatabase## Created tcuprice.db database fail: %s\n",pErrMsg);
		sqlite3_free(pErrMsg);
		sqlite3_close(price_db);
		price_db = NULL;

		return ERROR_FAIL;
	}

	sqlite3_free(pErrMsg);
	printf("##libdatabase## Created tcuprice.db success!\n");

	return ERROR_OK;
}

//创建计费模型
int protocol_creat_price_mode(charg_model* mode)
{
	char *pErrMsg = NULL;
	char m_starttime[15] = {0};
	char m_endtime[15] = {0};
	char m_modelID[17] = {0};
	char SQL_cmd[256] = {0};

	if(!mode)
		return ERROR_INPARA;

	/*
	 sprintf(m_modelID,"%02x%02x%02x%02x%02x%02x%02x%02x",mode->modelID[0],mode->modelID[1],mode->modelID[2],mode->modelID[3],\
		mode->modelID[4],mode->modelID[5],mode->modelID[6],mode->modelID[7]);

	 sprintf(m_starttime,"%02d%02d%02d%02d%02d%02d%02d",mode->starttime[0],mode->starttime[1],mode->starttime[2],mode->starttime[3],\
			mode->starttime[4],mode->starttime[5],mode->starttime[6]);
	 sprintf(m_endtime,"%02d%02d%02d%02d%02d%02d%02d",mode->endtime[0],mode->endtime[1],mode->endtime[2],mode->endtime[3],mode->endtime[4],\
		mode->endtime[5],mode->endtime[6]);
	 */
	sprintf(m_modelID,"%02x%02x%02x%02x%02x%02x%02x%02x",mode->modelID[7],mode->modelID[6],mode->modelID[5],mode->modelID[4],
			mode->modelID[3],mode->modelID[2],mode->modelID[1],mode->modelID[0]);

	sprintf(m_starttime,"20%02d%02d%02d%02d%02d%02d",mode->starttime[6],mode->starttime[5],mode->starttime[4],mode->starttime[3],
			mode->starttime[2],mode->starttime[1]);
	sprintf(m_endtime,"20%02d%02d%02d%02d%02d%02d",mode->endtime[6],mode->endtime[5],mode->endtime[4],mode->endtime[3],mode->endtime[2],
			mode->endtime[1]);

	sprintf(SQL_cmd,"insert into tagPrice values(%d,\'%s\',\'%s\',\'%s\',%d,%d,%d,\
		%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,\
		%d,%d,%d,%d,%d,%d,%d);",mode->guntab, m_modelID, m_starttime, m_endtime, mode->opstate, mode->metertype, mode->num, mode->time[0].time, mode->time[0].tag,
		mode->time[1].time, mode->time[1].tag, mode->time[2].time, mode->time[2].tag, mode->time[3].time, mode->time[3].tag, mode->time[4].time, mode->time[4].tag,
		mode->time[5].time, mode->time[5].tag, mode->time[6].time, mode->time[6].tag, mode->time[7].time, mode->time[7].tag, mode->time[8].time, mode->time[8].tag,
		mode->time[9].time, mode->time[9].tag, mode->time[10].time, mode->time[10].tag, mode->time[11].time, mode->time[11].tag, mode->rateprice[0],
		mode->rateprice[1], mode->rateprice[2], mode->rateprice[3], mode->rateprice[4], mode->rateprice[5], mode->rateprice[6]);

	if(sqlite3_exec(price_db,SQL_cmd,0,0,&pErrMsg) != SQLITE_OK) {
		printf("##libdatabase## insert tcuprice.db fail--->%s\n",pErrMsg);
		sqlite3_free(pErrMsg);
		sqlite3_close(price_db);
		price_db = NULL;

		return ERROR_FAIL;
	}

	printf("##libdatabase## insert tcup .db success! modelID:%s\n",m_modelID);
	sqlite3_free(pErrMsg);

	return ERROR_OK;
}

//更新计费模型

int protocol_update_price_mode(charg_model* mode)
{

	char *pErrMsg = NULL;
	char m_starttime[15] = {0};
	char m_endtime[15] = {0};
	char m_modelID[17] = {0};
	char SQL_cmd[512] = {0};

	if(!mode)
		return ERROR_INPARA;

	/*
	 sprintf(m_modelID,"%02x%02x%02x%02x%02x%02x%02x%02x",mode->modelID[0],mode->modelID[1],mode->modelID[2],mode->modelID[3],\
		mode->modelID[4],mode->modelID[5],mode->modelID[6],mode->modelID[7]);

	 sprintf(m_starttime,"%02d%02d%02d%02d%02d%02d%02d",mode->starttime[0],mode->starttime[1],mode->starttime[2],mode->starttime[3],\
			mode->starttime[4],mode->starttime[5],mode->starttime[6]);
	 sprintf(m_endtime,"%02d%02d%02d%02d%02d%02d%02d",mode->endtime[0],mode->endtime[1],mode->endtime[2],mode->endtime[3],mode->endtime[4],\
		mode->endtime[5],mode->endtime[6]);
	 */

	sprintf(m_modelID,"%02x%02x%02x%02x%02x%02x%02x%02x",mode->modelID[7],mode->modelID[6],mode->modelID[5],mode->modelID[4],
			mode->modelID[3],mode->modelID[2],mode->modelID[1],mode->modelID[0]);
	sprintf(m_starttime,"20%02d%02d%02d%02d%02d%02d",mode->starttime[6],mode->starttime[5],mode->starttime[4],mode->starttime[3],
			mode->starttime[2],mode->starttime[1]);
	sprintf(m_endtime,"20%02d%02d%02d%02d%02d%02d",mode->endtime[6],mode->endtime[5],mode->endtime[4],mode->endtime[3],mode->endtime[2],
			mode->endtime[1]);

	sprintf(SQL_cmd,"update tagPrice set guntab=%d, modelID=\'%s\', starttime=\'%s\', endtime=\'%s\', opstate=%d, metertype=%d, num=%d,\
		time_0=%2d, tag_0=%d, time_1=%2d, tag_1=%d, time_2=%2d, tag_2=%d, time_3=%2d, tag_3=%d, time_4=%2d, tag_4=%d, time_5=%2d, tag_5=%d, time_6=%2d,\
		tag_6=%d, time_7=%2d, tag_7=%d, time_8=%2d, tag_8=%d, time_9=%2d, tag_9=%d, time_10=%2d, tag_10=%d, time_11=%2d, tag_11=%2d,\
		rateprice_0=%d, rateprice_1=%d, rateprice_2=%d,rateprice_3=%d, rateprice_4=%d, rateprice_5=%d, rateprice_6=%d;",mode->guntab, m_modelID,
		m_starttime, m_endtime, mode->opstate, mode->metertype, mode->num, mode->time[0].time, mode->time[0].tag,
		mode->time[1].time, mode->time[1].tag, mode->time[2].time, mode->time[2].tag, mode->time[3].time, mode->time[3].tag, mode->time[4].time, mode->time[4].tag,
		mode->time[5].time, mode->time[5].tag, mode->time[6].time, mode->time[6].tag, mode->time[7].time, mode->time[7].tag, mode->time[8].time, mode->time[8].tag,
		mode->time[9].time, mode->time[9].tag, mode->time[10].time, mode->time[10].tag, mode->time[11].time, mode->time[11].tag, mode->rateprice[0],
		mode->rateprice[1], mode->rateprice[2], mode->rateprice[3], mode->rateprice[4], mode->rateprice[5], mode->rateprice[6]);

	if(sqlite3_exec(price_db,SQL_cmd,0,0,&pErrMsg) != SQLITE_OK) {
		printf("##libdatabase## update tcuprice.db fail--->%s\n",pErrMsg);
		sqlite3_free(pErrMsg);
		sqlite3_close(price_db);
		price_db = NULL;

		return ERROR_FAIL;
	}

	sqlite3_free(pErrMsg);
	printf("##libdatabase## update tcuprice.db success! m_modelID:%s\n",m_modelID);

	return ERROR_OK;
}

int protocol_updatePM(charg_model* mode)
{
	char *pErrMsg = NULL;
	char SQL_cmd[64] = {0};
	int nrow = 0, ncol = 0;
	char **pResult = NULL;
	int ret = 0;
	//	int i = 0;

	if(!mode)
		return ERROR_INPARA;

	ret = file_is_exist(PRICE_MODE_PATH,PRICE_DB_TYPE);
	if(ret == ERROR_INPARA)
		return ERROR_FAIL;
	else if(ret == ERROR_FAIL) {
		if(creat_price_mode_db() != ERROR_OK)
			return ERROR_FAIL;
	}

	if(price_db == NULL) {
		if(sqlite3_open(PRICE_MODE_PATH,&price_db) != SQLITE_OK) {
			sqlite3_close(price_db);
			price_db = NULL;

			return ERROR_FAIL;
		}
	}

#if 0
	printf("********* Database -> Price MOde Source Data *********\n");
	printf("mode->guntab = %d\n",mode->guntab);

	printf("mode->modelID = ");
	for(i=0; i<8; i++)
		printf("%02x ", mode->modelID[i]);
	printf("\n");

	printf("mode->starttime = ");
	for(i=0; i<7; i++)
		printf("%02x ",mode->starttime[i]);
	printf("\n");

	printf("mode->endtime = ");
	for(i=0; i<7; i++)
		printf("%02x ",mode->endtime[i]);
	printf("\n");

	printf("mode->opstate = %d\n",mode->opstate);
	printf("mode->metertype = %d\n",mode->metertype);
	printf("mode->num = %d\n",mode->num);

	for(i=0; i<12; i++)
	{
		printf("mode->time[%d] ,time = %d ,tag = %d\n",i,mode->time[i].time,mode->time[i].tag);
	}

	for(i=0; i<7; i++)
		printf("rateprice[i] = %d\n",mode->rateprice[i]);

#endif

	sprintf(SQL_cmd,"select * from tagPrice;");
	if(sqlite3_get_table(price_db,SQL_cmd,&pResult,&nrow,&ncol,&pErrMsg) != SQLITE_OK) {
		printf("##libdatabase## update tcuprice.db get info fail!-->%s\n",pErrMsg);
		sqlite3_free(pErrMsg);
		sqlite3_free_table(pResult);
		sqlite3_close(price_db);
		price_db = NULL;

		return ERROR_FAIL;
	}

	if(nrow == 0)
		ret = protocol_creat_price_mode(mode);
	else if(nrow == 1)
		ret = protocol_update_price_mode(mode);
	else {
		memset(SQL_cmd,0,sizeof(SQL_cmd));
		sprintf(SQL_cmd,"delete from tagPrice;");
		if(sqlite3_exec(price_db,SQL_cmd,0,0,&pErrMsg) != SQLITE_OK) {
			fprintf(stderr,"##libdatabase## %s fail-->%s\n",SQL_cmd,pErrMsg);
			sqlite3_free(pErrMsg);
			sqlite3_free_table(pResult);

			return ERROR_FAIL;
		}

		ret = protocol_creat_price_mode(mode);
		memset(SQL_cmd,0,sizeof(SQL_cmd));
		sprintf(SQL_cmd,"select * from tagPrice;");
	}
	sqlite3_exec(price_db,SQL_cmd,callback_func,0,&pErrMsg);
	sqlite3_free(pErrMsg);
	sqlite3_free_table(pResult);

	return ret;
}

int set_PM_value(int *ncol, int *index, char **pResult, charg_model *mode)
{
	int i = 0;
	char m_modelID[17] = {0};
	char m_starttime[15] = {0};
	char m_endtime[15] = {0};

	if(!ncol || !index || !pResult || !mode)
		return ERROR_INPARA;

	mode->guntab = (char)atoi(pResult[(*index) + PM_GUNTAB_OFFSET]);

	sprintf(m_modelID,pResult[(*index) + PM_MODELID_OFFSET]);
	for(i=0; i<8; i++)
		mode->modelID[i] = ((atoh(m_modelID[2*i]) << 4 ) | atoh(m_modelID[2*i+1]));

	sprintf(m_starttime,pResult[(*index) + PM_STARTTIME_OFFSET]);
	for(i=0; i<7; i++)
		mode->starttime[i] = ((atoh(m_starttime[2*i]) << 4 ) | atoh(m_starttime[2*i+1]));

	sprintf(m_endtime,pResult[(*index) + PM_ENDTIME_OFFSET]);
	for(i=0; i<7; i++)
		mode->endtime[i] = ((atoh(m_endtime[2*i]) << 4) | atoh(m_endtime[2*i+1]));

	mode->opstate = (short)atoi(pResult[(*index) + PM_OPSTATE_OFFSET]);
	mode->metertype = (short)atoi(pResult[(*index) + PM_METERTYPE_OFFSET]);
	mode->num = (char)atoi(pResult[(*index) + PM_NUM_OFFSET]);

	mode->time[0].time = (short)atoi(pResult[(*index) + PM_TIME0_OFFSET]);
	mode->time[0].tag = (short)atoi(pResult[(*index) + PM_TAG0_OFFSET]);
	mode->time[1].time = (short)atoi(pResult[(*index) + PM_TIME1_OFFSET]);
	mode->time[1].tag = (short)atoi(pResult[(*index) + PM_TAG1_OFFSET]);
	mode->time[2].time = (short)atoi(pResult[(*index) + PM_TIME2_OFFSET]);
	mode->time[2].tag = (short)atoi(pResult[(*index) + PM_TAG2_OFFSET]);
	mode->time[3].time = (short)atoi(pResult[(*index) + PM_TIME3_OFFSET]);
	mode->time[3].tag = (short)atoi(pResult[(*index) + PM_TAG3_OFFSET]);
	mode->time[4].time = (short)atoi(pResult[(*index) + PM_TIME4_OFFSET]);
	mode->time[4].tag = (short)atoi(pResult[(*index) + PM_TAG4_OFFSET]);
	mode->time[5].time = (short)atoi(pResult[(*index) + PM_TIME5_OFFSET]);
	mode->time[5].tag = (short)atoi(pResult[(*index) + PM_TAG5_OFFSET]);
	mode->time[6].time = (short)atoi(pResult[(*index) + PM_TIME6_OFFSET]);
	mode->time[6].tag = (short)atoi(pResult[(*index) + PM_TAG6_OFFSET]);
	mode->time[7].time = (short)atoi(pResult[(*index) + PM_TIME7_OFFSET]);
	mode->time[7].tag = (short)atoi(pResult[(*index) + PM_TAG7_OFFSET]);
	mode->time[8].time = (short)atoi(pResult[(*index) + PM_TIME8_OFFSET]);
	mode->time[8].tag = (short)atoi(pResult[(*index) + PM_TAG8_OFFSET]);
	mode->time[9].time = (short)atoi(pResult[(*index) + PM_TIME9_OFFSET]);
	mode->time[9].tag = (short)atoi(pResult[(*index) + PM_TAG9_OFFSET]);
	mode->time[10].time = (short)atoi(pResult[(*index) + PM_TIME10_OFFSET]);
	mode->time[10].tag = (short)atoi(pResult[(*index) + PM_TAG10_OFFSET]);
	mode->time[11].time = (short)atoi(pResult[(*index) + PM_TIME11_OFFSET]);
	mode->time[11].tag = (short)atoi(pResult[(*index) + PM_TAG11_OFFSET]);

	mode->rateprice[0] = atoi(pResult[(*index) + PM_RATEPRICE0_OFFSET]);
	mode->rateprice[1] = atoi(pResult[(*index) + PM_RATEPRICE1_OFFSET]);
	mode->rateprice[2] = atoi(pResult[(*index) + PM_RATEPRICE2_OFFSET]);
	mode->rateprice[3] = atoi(pResult[(*index) + PM_RATEPRICE3_OFFSET]);
	mode->rateprice[4] = atoi(pResult[(*index) + PM_RATEPRICE4_OFFSET]);
	mode->rateprice[5] = atoi(pResult[(*index) + PM_RATEPRICE5_OFFSET]);
	mode->rateprice[6] = atoi(pResult[(*index) + PM_RATEPRICE6_OFFSET]);

	return ERROR_OK;

}

int protocol_queryPM(charg_model* mode)
{

	char *pErrMsg = NULL;
	char SQL_cmd[64] = {0};
	int nrow = 0, ncol = 0;
	char **pResult = NULL;
	int index = 0;

	if(!mode)
		return ERROR_INPARA;

	if(file_is_exist(PRICE_MODE_PATH,PRICE_DB_TYPE) != ERROR_OK)
		if(creat_price_mode_db() != ERROR_OK)
			return ERROR_FAIL;

	if(price_db == NULL) {
		if(sqlite3_open(PRICE_MODE_PATH,&price_db) != SQLITE_OK) {
			sqlite3_close(price_db);
			config_db = NULL;

			return ERROR_FAIL;
		}
	}

	sprintf(SQL_cmd,"select * from tagPrice;");
	if(sqlite3_get_table(price_db,SQL_cmd,&pResult,&nrow,&ncol,&pErrMsg) == SQLITE_OK) {
		if(nrow == 0)
			return ERROR_FAIL;
		index = ncol;
		if(set_PM_value(&ncol,&index,pResult,mode) != ERROR_OK) {
			sqlite3_free(pErrMsg);
			sqlite3_free_table(pResult);
			return ERROR_FAIL;
		}
	}
	else {
		printf("##libdatabase## query tcuprice.db get table info fail--->%s\n",pErrMsg);
		sqlite3_free(pErrMsg);
		sqlite3_free_table(pResult);
		sqlite3_close(price_db);
		price_db = NULL;
		return ERROR_FAIL;
	}

	sqlite3_free(pErrMsg);
	sqlite3_free_table(pResult);
	printf("##libdatabase## query tcuprice.db success!\n");
	return ERROR_OK;
}
#endif

/***********管理卡部分************/
static int creat_admin_card_db(void) {
	DIR *dp = NULL;
	char *pErrMsg = NULL;
	const char* SQL_cmd =
			"create table if not exists tagCard([card_id] TEXT(17) PRIMARY KEY,[card_type] TEXT(2),[card_order] INTEGER);";

	if ((dp = opendir(DB_DIR_PATH)) == NULL)
		if (mkdir(DB_DIR_PATH, S_IRWXU | S_IRWXG | S_IRWXO) != 0) {
			card_db = NULL;
			closedir(dp);
			return ERROR_FAIL;
		}
	closedir(dp);

	if (access(CARD_PATH, F_OK) != 0) {
		if (creat(CARD_PATH, O_WRONLY | O_TRUNC) == -1) {
			card_db = NULL;
			return ERROR_FAIL;
		}
		chmod(CARD_PATH, S_IRWXU | S_IRWXG | S_IRWXO);
	}

	if (card_db == NULL)
		if (sqlite3_open(CARD_PATH, &card_db) != SQLITE_OK) {
			sqlite3_close(card_db);
			card_db = NULL;

			return ERROR_FAIL;
		}

	if (sqlite3_exec(card_db, SQL_cmd, 0, 0, &pErrMsg) != SQLITE_OK) {
		fprintf(stderr,
				"##libdatabase## Created tcuprice.db database fail: %s\n",
				pErrMsg);
		sqlite3_free(pErrMsg);
		sqlite3_close(card_db);
		card_db = NULL;

		return ERROR_FAIL;
	}

	sqlite3_free(pErrMsg);
	printf("##libdatabase## Created tcuprice.db success!\n");

	return ERROR_OK;
}

static int protocol_creat_admin_card(admin_card* card) {
	char *pErrMsg = NULL;
	char SQL_cmd[1024] = { 0 };
	char card_id[17];

	if (!card)
		return ERROR_INPARA;

	sprintf(card_id, "%02x%02x%02x%02x%02x%02x%02x%02x", card->card_id[0],
			card->card_id[1], card->card_id[2], card->card_id[3],
			card->card_id[4], card->card_id[5], card->card_id[6],
			card->card_id[7]);

	sprintf(SQL_cmd, "insert into tagCard values(\"%s\",\"%x\",%d);", card_id,
			card->card_type, card->card_order);

	if (sqlite3_exec(card_db, SQL_cmd, 0, 0, &pErrMsg) != SQLITE_OK) {
		fprintf(stderr,
				"##libdatabase## insert tagCard.db fail! reason:%s card_id:%s\n",
				pErrMsg, card_id);
		sqlite3_free(pErrMsg);
		sqlite3_close(card_db);
		card_db = NULL;
		return ERROR_FAIL;
	}

	printf("##libdatabase## insert tcucard.db success! card_id:%s\n", card_id);
	sqlite3_free(pErrMsg);
	return ERROR_OK;
}

static int protocol_delete_admin_card(admin_card* card) {
	char *pErrMsg = NULL;
	char SQL_cmd[1024] = { 0 };
	char card_id[17];

	if (!card)
		return ERROR_INPARA;

	sprintf(card_id, "%02x%02x%02x%02x%02x%02x%02x%02x", card->card_id[0],
			card->card_id[1], card->card_id[2], card->card_id[3],
			card->card_id[4], card->card_id[5], card->card_id[6],
			card->card_id[7]);

	sprintf(SQL_cmd, "delete from tagCard where card_id=\'%s\';", card_id);

	if (sqlite3_exec(card_db, SQL_cmd, 0, 0, &pErrMsg) != SQLITE_OK) {
		fprintf(stderr,
				"##libdatabase## delete from tagCard fail!,card_id:%s  reason:%s\n",
				card_id, pErrMsg);
		sqlite3_free(pErrMsg);
		sqlite3_close(card_db);
		card_db = NULL;

		return ERROR_FAIL;
	}

	printf("##libdatabase## delete from tagCard success!, card_id:%s\n",
			card_id);
	sqlite3_free(pErrMsg);

	return ERROR_OK;
}

//card是指向admin_card的指针,operation=1表示插入，operation=2表示删除，成功返回ERROR_OK
int protocol_updateCard(admin_card* card, char operation) {
	int ret = 0;
	char* pErrMsg = NULL;
	char SQL_cmd[128] = { 0 };
	char card_id[17] = { 0 };

	if (!card || ((operation != 1) && (operation != 2)))
		return ERROR_INPARA;

	ret = file_is_exist(CARD_PATH, CARD_DB_TYPE);
	if (ret == ERROR_INPARA)
		return ERROR_FAIL;
	else if (ret == ERROR_FAIL) {
		if (creat_admin_card_db() != ERROR_OK)
			return ERROR_FAIL;
	}

	if (card_db == NULL) {
		if (sqlite3_open(CARD_PATH, &card_db) != SQLITE_OK) {
			sqlite3_close(card_db);
			card_db = NULL;

			return ERROR_FAIL;
		}
	}

	sprintf(card_id, "%02x%02x%02x%02x%02x%02x%02x%02x", card->card_id[0],
			card->card_id[1], card->card_id[2], card->card_id[3],
			card->card_id[4], card->card_id[5], card->card_id[6],
			card->card_id[7]);

	if (operation == 1) {
		ret = protocol_creat_admin_card(card);
		sprintf(SQL_cmd, "select * from tagCard where card_id=\"%s\";",
				card_id);
		sqlite3_exec(card_db, SQL_cmd, callback_func, 0, &pErrMsg);
	} else if (operation == 2)
		ret = protocol_delete_admin_card(card);
	else {
		printf("##libdatabase##protocol_updateCard ,operation value error!\n");
		return ERROR_FAIL;
	}

	return ret;
}

static int set_Card_value(int *ncol, int *index, char **pResult, admin_card* card) {
	int i = 0;
	char m_id[17] = { 0 };

	if (!ncol || !index || !pResult || !card)
		return ERROR_INPARA;

	snprintf(m_id, 17, "%s", pResult[(*index) + ADMIN_CARD_ID_OFFSET]);
	for (i = 0; i < 8; i++)
		card->card_id[i] = (atoh(m_id[i * 2]) << 4 | atoh(m_id[i * 2 + 1]));

	card->card_type = (char) atoh(*pResult[(*index) + ADMIN_CARD_TYPE_OFFSET]);
	card->card_order = (char) atoi(pResult[(*index) + ADMIN_CARD_ORDER_OFFSET]);

	(*index) = (*index) + (*ncol);
	return ERROR_OK;
}

//card是admin的数组，大小为5个admin_card大小,成功返回ERROR_OK
int protocol_queryCard(admin_card* card) {
	char *pErrMsg = NULL;
	char SQL_cmd[64] = { 0 };
	int nrow = 0, ncol = 0;
	char **pResult = NULL;
	int index = 0;
	int i = 0;

	if (!card)
		return ERROR_INPARA;

	if (file_is_exist(CARD_PATH, CARD_DB_TYPE) != ERROR_OK)
		if (creat_admin_card_db() != ERROR_OK)
			return ERROR_FAIL;

	if (card_db == NULL) {
		if (sqlite3_open(CARD_PATH, &card_db) != SQLITE_OK) {
			sqlite3_close(card_db);
			card_db = NULL;

			return ERROR_FAIL;
		}
	}

	sprintf(SQL_cmd, "select * from tagCard;");
	if (sqlite3_get_table(card_db, SQL_cmd, &pResult, &nrow, &ncol,
			&pErrMsg) == SQLITE_OK) {
		if (nrow == 0)
			return ERROR_FAIL;
		index = ncol;
		for (i = 0; i < nrow; i++)
			if (set_Card_value(&ncol, &index, pResult, &card[i]) != ERROR_OK) {
				sqlite3_free(pErrMsg);
				sqlite3_free_table(pResult);
				return ERROR_FAIL;
			}
	} else {
		printf("##libdatabase## query tcucard.db get table info fail--->%s\n",
				pErrMsg);
		sqlite3_free(pErrMsg);
		sqlite3_free_table(pResult);
		sqlite3_close(card_db);
		card_db = NULL;
		return ERROR_FAIL;
	}

	sqlite3_free(pErrMsg);
	sqlite3_free_table(pResult);
	printf("##libdatabase## query tcucard.db success!\n");
	return ERROR_OK;
}

static int creat_blacklist_card_db() {
	DIR *dp = NULL;
	char *pErrMsg = NULL;
	const char* SQL_cmd =
			"create table if not exists tagBlackCard([card_num] TEXT(17) PRIMARY KEY,[state] INTEGER);";

	if ((dp = opendir(DB_DIR_PATH)) == NULL)
		if (mkdir(DB_DIR_PATH, S_IRWXU | S_IRWXG | S_IRWXO) != 0) {
			blacklistCard_db = NULL;
			closedir(dp);
			return ERROR_FAIL;
		}
	closedir(dp);

	if (access(BLACKLIST_CARD_PATH, F_OK) != 0) {
		if (creat(BLACKLIST_CARD_PATH, O_WRONLY | O_TRUNC) == -1) {
			blacklistCard_db = NULL;
			return ERROR_FAIL;
		}
		chmod(BLACKLIST_CARD_PATH, S_IRWXU | S_IRWXG | S_IRWXO);
	}

	if (blacklistCard_db == NULL)
		if (sqlite3_open(BLACKLIST_CARD_PATH, &blacklistCard_db) != SQLITE_OK) {
			sqlite3_close(blacklistCard_db);
			blacklistCard_db = NULL;

			return ERROR_FAIL;
		}

	if (sqlite3_exec(blacklistCard_db, SQL_cmd, 0, 0, &pErrMsg) != SQLITE_OK) {
		fprintf(stderr,
				"##libdatabase## Created tcublacklistcard.db database fail: %s\n",
				pErrMsg);
		sqlite3_free(pErrMsg);
		sqlite3_close(blacklistCard_db);
		blacklistCard_db = NULL;

		return ERROR_FAIL;
	}

	sqlite3_free(pErrMsg);
	printf("##libdatabase## Created tcublacklistcard.db success!\n");

	return ERROR_OK;
}

static int insert_blacklist_card(blacklist_card* card) {
	char *pErrMsg = NULL;
	char SQL_cmd[1024] = { 0 };
	char card_id[17];

	if (!card)
		return ERROR_INPARA;

	sprintf(card_id, "%02x%02x%02x%02x%02x%02x%02x%02x", card->cardNum[0],
			card->cardNum[1], card->cardNum[2], card->cardNum[3],
			card->cardNum[4], card->cardNum[5], card->cardNum[6],
			card->cardNum[7]);

	sprintf(SQL_cmd, "insert into tagBlackCard values(\"%s\",%d);", card_id,
			card->state);

	if (sqlite3_exec(blacklistCard_db, SQL_cmd, 0, 0, &pErrMsg) != SQLITE_OK) {
		fprintf(stderr,
				"##libdatabase## insert tcublacklistcard.db fail! card_id:%s  reason:%s \n",
				card_id, pErrMsg);
		sqlite3_free(pErrMsg);
		sqlite3_close(blacklistCard_db);
		blacklistCard_db = NULL;
		return ERROR_FAIL;
	}

	printf("##libdatabase## insert tcucard.db success! card_id:%s\n", card_id);

	return ERROR_OK;
}

static int delete_blacklist_card(blacklist_card* card) {
	char *pErrMsg = NULL;
	char SQL_cmd[1024] = { 0 };
	char card_id[17];

	if (!card)
		return ERROR_INPARA;

	sprintf(card_id, "%02x%02x%02x%02x%02x%02x%02x%02x", card->cardNum[0],
			card->cardNum[1], card->cardNum[2], card->cardNum[3],
			card->cardNum[4], card->cardNum[5], card->cardNum[6],
			card->cardNum[7]);

	sprintf(SQL_cmd, "delete from tagBlackCard where card_num=\'%s\';",
			card_id);

	if (sqlite3_exec(blacklistCard_db, SQL_cmd, 0, 0, &pErrMsg) != SQLITE_OK) {
		fprintf(stderr,
				"##libdatabase## delete from tagBlackCard fail!,card_num:%s  reason:%s\n",
				card_id, pErrMsg);
		sqlite3_free(pErrMsg);
		sqlite3_close(blacklistCard_db);
		blacklistCard_db = NULL;

		return ERROR_FAIL;
	}

	printf("##libdatabase## delete from tagBlackCard success!, card_num:%s\n",
			card_id);

	return ERROR_OK;
}
//黑名单卡更新
int protocol_update_blacklistCard(blacklist_card* card) {
	int ret = 0;
	char* pErrMsg = NULL;
	char SQL_cmd[128] = { 0 };
	char m_cardNum[17] = { 0 };
	char m_tmp[3] = { 0 };
	int i = 0;
	int nrow = 0, ncolnum = 0;
	char **pResult = NULL;

	if (card == NULL) {
		printf("##libdatabase## update blacklist fail, due to para is NULL!!!");
		return ERROR_INPARA;
	}

	ret = file_is_exist(BLACKLIST_CARD_PATH, BLACKLIST_CARD_TYPE);
	if (ret == ERROR_INPARA)
		return ERROR_FAIL;
	else if (ret == ERROR_FAIL) {
		if (creat_blacklist_card_db() != ERROR_OK)
			return ERROR_FAIL;
	}

	if (blacklistCard_db == NULL) {
		if (sqlite3_open(BLACKLIST_CARD_PATH, &blacklistCard_db) != SQLITE_OK) {
			sqlite3_close(blacklistCard_db);
			blacklistCard_db = NULL;

			return ERROR_FAIL;
		}
	}

	for (i = 0; i < 8; i++) {
		memset(m_tmp, 0, 3);
		sprintf(m_tmp, "%02x", card->cardNum[i]);
		strcat(m_cardNum, m_tmp);
	}

	sprintf(SQL_cmd, "select * from   tagBlackCard where card_num=\'%s\';",
			m_cardNum);
	if (sqlite3_get_table(blacklistCard_db, SQL_cmd, &pResult, &nrow, &ncolnum,
			&pErrMsg) != SQLITE_OK) {
		printf(
				"##libdatabase## update tcublacklistcard.db get table info fail!--->%s\n",
				pErrMsg);
		sqlite3_free(pErrMsg);
		sqlite3_free_table(pResult);
		sqlite3_close(blacklistCard_db);
		blacklistCard_db = NULL;

		return ERROR_FAIL;
	}

	if (nrow == 0) {
		if (card->state == 1)
			ret = insert_blacklist_card(card);
	} else if (nrow == 1) {
		if (card->state == 2)
			ret = delete_blacklist_card(card);
	} else {
		ret = delete_blacklist_card(card);
		if (card->state == 1)
			ret = insert_blacklist_card(card);
	}

	sqlite3_free(pErrMsg);
	sqlite3_free_table(pResult);
	printf("blacklist cardNum\t%s\n", m_cardNum);
	printf("blacklist state\t\t%d\n", card->state);
	return ret;
}
//卡号是否存在于黑名单中 0不存在  1存在
int protocol_blacklistCard_isExist(char cardNum[8]) {
	int ret = 0;
	char m_cardNum[17] = { 0 };
	char m_tmp[3] = { 0 };
	int i = 0;
	int nrow = 0, ncolnum = 0;
	char **pResult = NULL;
	char SQL_cmd[128] = { 0 };
	char* pErrMsg = NULL;

	if (cardNum == NULL) {
		printf(
				"##libdatabase## pending blacklist fail, due to para is NULL!!!");
		return 1;
	}

	ret = file_is_exist(BLACKLIST_CARD_PATH, BLACKLIST_CARD_TYPE);
	if (ret == ERROR_INPARA)
		return ERROR_INPARA;
	else if (ret == ERROR_FAIL) {
		if (creat_blacklist_card_db() != ERROR_OK)
			return ERROR_FAIL;
	}

	if (blacklistCard_db == NULL) {
		if (sqlite3_open(BLACKLIST_CARD_PATH, &blacklistCard_db) != SQLITE_OK) {
			sqlite3_close(blacklistCard_db);
			blacklistCard_db = NULL;
			return ERROR_FAIL;
		}
	}

	for (i = 0; i < 8; i++) {
		memset(m_tmp, 0, 3);
		sprintf(m_tmp, "%02x", cardNum[i]);
		strcat(m_cardNum, m_tmp);
	}

	sprintf(SQL_cmd, "select * from   tagBlackCard where card_num=\'%s\';",
			m_cardNum);
	if (sqlite3_get_table(blacklistCard_db, SQL_cmd, &pResult, &nrow, &ncolnum,
			&pErrMsg) != SQLITE_OK) {
		printf(
				"##libdatabase## update tcublacklistcard.db get table info fail!--->%s\n",
				pErrMsg);
		sqlite3_free(pErrMsg);
		sqlite3_free_table(pResult);
		sqlite3_close(blacklistCard_db);
		blacklistCard_db = NULL;

		return ERROR_FAIL;
	}

	if (nrow != 0) {
		sqlite3_free(pErrMsg);
		sqlite3_free_table(pResult);
		printf("##libdatabase## %s in blacklist!!!\n", m_cardNum);
		return 1;
	}
	sqlite3_free(pErrMsg);
	sqlite3_free_table(pResult);
	printf("##libdatabase## %s dosen't in blacklist!!!\n", m_cardNum);
	return 0;
}

static int creat_guninfo_db(){
	DIR *dp = NULL;
	char *pErrMsg = NULL;
	char SQL_cmd[256] ={0};

	if ((dp = opendir(DB_DIR_PATH)) == NULL)
		if (mkdir(DB_DIR_PATH, S_IRWXU | S_IRWXG | S_IRWXO) != 0) {
			guninfo_db = NULL;
			closedir(dp);
			return ERROR_FAIL;
		}
	closedir(dp);

	if (access(GUNINFO_PATH, F_OK) != 0) {
		if (creat(GUNINFO_PATH, O_WRONLY | O_TRUNC) == -1) {
			guninfo_db = NULL;
			return ERROR_FAIL;
		}
		chmod(GUNINFO_PATH, S_IRWXU | S_IRWXG | S_IRWXO);
	}

	if (guninfo_db == NULL)
		if (sqlite3_open(GUNINFO_PATH, &guninfo_db) != SQLITE_OK) {
			sqlite3_close(guninfo_db);
			guninfo_db = NULL;

			return ERROR_FAIL;
		}

	sprintf(SQL_cmd,"create table if not exists %s([gunno]  int PRIMARY KEY, [can_port] int, [frame_gunno] int,"
			"[cha_addr] int, [mt_port] int, [phase_num] int, [meteraddr] varchar(13), [ct] int);",TABLE_GUNINFO_NAME);

	if (sqlite3_exec(guninfo_db, SQL_cmd, 0, 0, &pErrMsg) != SQLITE_OK) {
		fprintf(stderr,
				"##libdatabase## Created tcuguninfo.db database fail: %s\n",pErrMsg);
		sqlite3_free(pErrMsg);
		sqlite3_close(guninfo_db);
		guninfo_db = NULL;

		return ERROR_FAIL;
	}

	sqlite3_free(pErrMsg);
	printf("##libdatabase## Created tcuguninfo.db success!\n");

	return ERROR_OK;
}

static int insert_guninfo_db(Gun_Info* gun_info){
	char *pErrMsg = NULL;
	char SQL_cmd[512] = { 0 };
	char m_meteraddr[13] = {0};
	char m_tmp[3]={0};
	char i = 0;

	if (!gun_info)
		return ERROR_INPARA;

	for(i=0; i<6; i++){
		memset(m_tmp,0,3);
		sprintf(m_tmp,"%02x",gun_info->meteraddr[i]);
		strcat(m_meteraddr,m_tmp);
	}

	sprintf(SQL_cmd, "insert into %s values(%d, %d, %d, %d, %d, %d, \'%s\', %d);", TABLE_GUNINFO_NAME,gun_info->gunno,gun_info->can_port,
			gun_info->frame_gunno,gun_info->cha_addr, gun_info->mt_port, gun_info->phase_num,m_meteraddr,gun_info->ct);

	if (sqlite3_exec(guninfo_db, SQL_cmd, 0, 0, &pErrMsg) != SQLITE_OK) {
		fprintf(stderr,"##libdatabase## insert tcuguninfo.db fail! reason:%s \n",pErrMsg);
		sqlite3_free(pErrMsg);
		sqlite3_close(guninfo_db);
		guninfo_db = NULL;
		return ERROR_FAIL;
	}

	printf("##libdatabase## insert tcuguninfo.db success! gunno:%d\n", gun_info->gunno);
	sqlite3_free(pErrMsg);
	return ERROR_OK;
}

static int update_guninfo_db(Gun_Info* gun_info){
	char *pErrMsg = NULL;
	char SQL_cmd[512] = { 0 };
	char m_meteraddr[13] = {0};
	char m_tmp[3]={0};
	char i = 0;

	if (!gun_info)
		return ERROR_INPARA;

	for(i=0; i<6; i++){
		memset(m_tmp,0,3);
		sprintf(m_tmp,"%02x",(char)gun_info->meteraddr[i]);
		strcat(m_meteraddr,m_tmp);
	}

	sprintf(SQL_cmd,"update %s set can_port=%d,  frame_gunno=%d, cha_addr=%d, mt_port=%d, phase_num=%d,"
			" meteraddr=\'%s\', ct=%d  where gunno=%d; ",TABLE_GUNINFO_NAME,gun_info->can_port,gun_info->frame_gunno,
			gun_info->cha_addr,gun_info->mt_port,gun_info->phase_num,m_meteraddr,gun_info->ct,gun_info->gunno);

	if (sqlite3_exec(guninfo_db, SQL_cmd, 0, 0, &pErrMsg) != SQLITE_OK) {
		fprintf(stderr,"##libdatabase## update  tcuguninfo.db fail! reason:%s  gunno:%d\n",pErrMsg,gun_info->gunno);
		sqlite3_free(pErrMsg);
		sqlite3_close(guninfo_db);
		guninfo_db = NULL;
		return ERROR_FAIL;
	}

	printf("##libdatabase## update tcuguninfo.db success! gunno:%d\n", gun_info->gunno);
	sqlite3_free(pErrMsg);

	return ERROR_OK;
}

static int delete_gunifo_db(unsigned char gunno){
	char *pErrMsg = NULL;
	char SQL_cmd[128] = { 0 };

	sprintf(SQL_cmd, "delete from %s where gunno=%d",TABLE_GUNINFO_NAME,gunno);
	if (sqlite3_exec(guninfo_db, SQL_cmd, 0, 0, &pErrMsg) != SQLITE_OK) {
		fprintf(stderr,"##libdatabase## delete from %s fail!,gunno:%d  reason:%s\n",TABLE_GUNINFO_NAME,gunno, pErrMsg);
		sqlite3_free(pErrMsg);
		sqlite3_close(guninfo_db);
		guninfo_db = NULL;

		return ERROR_FAIL;
	}

	printf("##libdatabase## delete from %s success!, gunno:%d\n",TABLE_GUNINFO_NAME,gunno);

	return ERROR_OK;
}

static void create_default_guninfo_db(){
	Gun_Info gun[8];
	int i=0, ret=0;

	printf("creat default gun config information...\n ");

	memset(gun,0,sizeof(gun));
	for(i=0; i<8; i++){
		gun[i].gunno = i;
		gun[i].frame_gunno=i;
		gun[i].cha_addr = 0xf6;
		gun[i].phase_num=1;
		gun[i].ct = 1000;
		memset(gun[i].meteraddr,0xAA,6);
	}

	gun[0].mt_port=1;
	gun[0].can_port=0;
	gun[1].mt_port=2;
	gun[1].can_port=1;

	for(i=0; i<8; i++){
		if((ret=insert_guninfo_db(&gun[i]) )  != ERROR_OK){
			break;
		}
	}

}

static void StrToHex(unsigned char *pbDest, char *pbSrc, int nLen)
{
	char h1,h2;
	unsigned char s1,s2;
	int i;

	for (i=0; i<nLen; i++)
	{
		h1 = pbSrc[2*i];
		h2 = pbSrc[2*i+1];

		s1 = toupper(h1) - 0x30;
		if (s1 > 9)
			s1 -= 7;

		s2 = toupper(h2) - 0x30;
		if (s2 > 9)
			s2 -= 7;

		pbDest[i] = s1*16 + s2;
	}
}

static int set_guninfo_value(int *ncol, int *index, char **pResult, Gun_Info* gun){
	//	int i = 0;
	char m_meteraddr[13] = { 0 };

	if (!ncol || !index || !pResult || !gun)
		return ERROR_INPARA;

	gun->gunno =  atoi(pResult[(*index) + GUN_GUNNO_OFFSET]);
	gun->can_port =  atoi(pResult[(*index) + GUN_CAN_PORT_OFFSET]);
	gun->frame_gunno =  atoi(pResult[(*index) + GUN_FRAME_GUNNO_OFFSET]);
	gun->cha_addr =  atoi(pResult[(*index) + GUN_CHA_ADDR_OFFSET]);
	gun->mt_port =  atoi(pResult[(*index) + GUN_MT_PORT_OFFSET]);
	gun->phase_num =  atoi(pResult[(*index) + GUN_PHASE_NUM_OFFSET]);

	snprintf(m_meteraddr, 13, "%s", pResult[(*index) + GUN_METERADDR_OFFSET]);
	StrToHex(gun->meteraddr,m_meteraddr,sizeof(gun->meteraddr));
	//	printf("##libdatabase## meteraddr:%s\n",m_meteraddr);
	//	for (i = 0; i < 6; i++){
	////		gun->meteraddr[i] = (atoh(m_meteraddr[i * 2]) << 4 | atoh(m_meteraddr[i*2+1]));
	//		printf("%02x",gun->meteraddr[i]);
	//	}
	//	printf("\n");

	gun->ct =  atoi(pResult[(*index) + GUN_CT_OFFSET]);

	(*index) = (*index) + (*ncol);
	return ERROR_OK;
}

//更新、插入充电枪信息
int protocol_update_GunInfo( Gun_Info* gun_info){
	int ret = 0;
	char* pErrMsg = NULL;
	char SQL_cmd[128] = { 0 };
	int nrow = 0, ncolnum = 0;
	char **pResult = NULL;

	if (!gun_info)
		return ERROR_INPARA;

	ret = file_is_exist(GUNINFO_PATH, GUNINFO_DB_TYPE);
	if (ret == ERROR_INPARA)
		return ERROR_FAIL;
	else if (ret == ERROR_FAIL) {
		if (creat_guninfo_db() != ERROR_OK)
			return ERROR_FAIL;
	}

	if (gun_info == NULL) {
		if (sqlite3_open(CARD_PATH, &guninfo_db) != SQLITE_OK) {
			sqlite3_close(guninfo_db);
			guninfo_db = NULL;

			return ERROR_FAIL;
		}
	}

	sprintf(SQL_cmd, "select * from %s where gunno=%d;",TABLE_GUNINFO_NAME, gun_info->gunno);
	printf("%s\n",SQL_cmd);

	if (sqlite3_get_table(guninfo_db, SQL_cmd, &pResult, &nrow, &ncolnum,&pErrMsg) != SQLITE_OK) {
		printf("##libdatabase## update tcuguninfo.db get table info fail!--->%s\n",pErrMsg);
		sqlite3_free(pErrMsg);
		sqlite3_free_table(pResult);
		sqlite3_close(guninfo_db);
		guninfo_db = NULL;

		return ERROR_FAIL;
	}

	if (nrow == 0) {
		ret = insert_guninfo_db(gun_info);
	} else if (nrow == 1) {
		ret = update_guninfo_db(gun_info);
	} else {
		ret = delete_gunifo_db(gun_info->gunno);
		if(ret == ERROR_OK)
			ret = insert_guninfo_db(gun_info);
	}

	if(ret == ERROR_OK){
		sqlite3_exec(guninfo_db, SQL_cmd, callback_func, 0, &pErrMsg);
	}

	sqlite3_free(pErrMsg);
	sqlite3_free_table(pResult);

	return ret;
}
//获取充电枪信息
int protocol_get_GunInfo(unsigned char gunno, Gun_Info* gun_info){
	int ret = 0;
	char* pErrMsg = NULL;
	char SQL_cmd[128] = { 0 };
	int nrow = 0, ncolnum = 0 ,index=0;
	char **pResult = NULL;
	Gun_Info gun;

	if (!gun_info)
		return ERROR_INPARA;

	ret = file_is_exist(GUNINFO_PATH, GUNINFO_DB_TYPE);
	if (ret == ERROR_INPARA)
		return ERROR_FAIL;
	else if (ret == ERROR_FAIL) {
		if (creat_guninfo_db() != ERROR_OK)
			return ERROR_FAIL;
		create_default_guninfo_db();
	}

	if (guninfo_db == NULL) {
		if (sqlite3_open(GUNINFO_PATH, &guninfo_db) != SQLITE_OK) {
			sqlite3_close(guninfo_db);
			guninfo_db = NULL;

			return ERROR_FAIL;
		}
	}

	sprintf(SQL_cmd, "select * from %s where gunno=%d;",TABLE_GUNINFO_NAME, gunno);
	if (sqlite3_get_table(guninfo_db, SQL_cmd, &pResult, &nrow, &ncolnum,&pErrMsg) != SQLITE_OK) {
		printf("##libdatabase## protocol_get_GunInfo tcuguninfo.db get table info fail!--->%s\n",pErrMsg);
		sqlite3_free(pErrMsg);
		sqlite3_free_table(pResult);
		sqlite3_close(guninfo_db);
		guninfo_db = NULL;
		printf("##libdatabase## get gunno:%d info  failed!, due to query databse failed!\n",gunno);
		return ERROR_FAIL;
	}

	if (nrow == 0)
		return ERROR_FAIL;

	index = ncolnum;
	memset(&gun,0,sizeof(Gun_Info));
	if (set_guninfo_value(&ncolnum, &index, pResult, &gun) != ERROR_OK) {
		sqlite3_free(pErrMsg);
		sqlite3_free_table(pResult);
		printf("##libdatabase## get gunno:%d info  failed!, due to set value error!\n",gunno);
		return ERROR_FAIL;
	}

	sqlite3_free(pErrMsg);
	sqlite3_free_table(pResult);

	memcpy(gun_info,&gun,sizeof(Gun_Info));
		printf("##libdatabase## get gunno:%d info  success!\n",gunno);
		printf("src_gunno:%d  dst_gunno:%d\n",gun.gunno,gun_info->gunno);
		printf("src_can_port:%d  dst_can_port:%d\n",gun.can_port,gun_info->can_port);
		printf("src_frame_gunno:%d  dst_frame_gunno:%d\n",gun.frame_gunno,gun_info->frame_gunno);
		printf("src_cha_addr:%d  dst_cha_addr:%d\n",gun.cha_addr,gun_info->cha_addr);
		printf("src_mt_port:%d  dst_mt_port:%d\n",gun.mt_port,gun_info->mt_port);
		printf("src_phase_num:%d  dst_phase_num:%d\n",gun.phase_num,gun_info->phase_num);
		printf("src_ct:%d  dst_ct:%d\n",gun.ct,gun_info->ct);
		int i = 0;
		printf("src_meteraddr:");
		for(i=0;i<6; i++)
			printf("%02x",gun.meteraddr[i]);
	//	printf("\tdst_meteraddr:");
	//	for(i=0;i<6; i++)
	//		printf("%02x",gun_info->meteraddr[i]);
		printf("\n");
		printf("#####################################\n");

	return ERROR_OK;
}
//删除充电枪信息
int protocol_delete_GunInfo(unsigned char gunno){
	int ret = 0;

	ret = file_is_exist(GUNINFO_PATH, GUNINFO_DB_TYPE);
	if (ret == ERROR_INPARA)
		return ERROR_FAIL;
	else if (ret == ERROR_FAIL) {
		if (creat_guninfo_db() != ERROR_OK)
			return ERROR_FAIL;
		create_default_guninfo_db();
	}

	if (guninfo_db == NULL) {
		if (sqlite3_open(GUNINFO_PATH, &guninfo_db) != SQLITE_OK) {
			sqlite3_close(guninfo_db);
			guninfo_db = NULL;

			return ERROR_FAIL;
		}
	}

	ret = delete_gunifo_db(gunno);
	return ret;
}


static int create_displaycode_db()
{
	DIR *dp = NULL;
	char *pErrMsg = NULL;
	char SQL_cmd[256] ={0};

	if ((dp = opendir(DB_DIR_PATH)) == NULL)
		if (mkdir(DB_DIR_PATH, S_IRWXU | S_IRWXG | S_IRWXO) != 0) {
			code_db = NULL;
			closedir(dp);
			return ERROR_FAIL;
		}
	closedir(dp);

	if (access(CODE_PATH, F_OK) != 0) {
		if (creat(CODE_PATH, O_WRONLY | O_TRUNC) == -1) {
			code_db = NULL;
			return ERROR_FAIL;
		}
		chmod(GUNINFO_PATH, S_IRWXU | S_IRWXG | S_IRWXO);
	}

	if (code_db == NULL)
		if (sqlite3_open(CODE_PATH, &code_db) != SQLITE_OK) {
			sqlite3_close(code_db);
			code_db = NULL;

			return ERROR_FAIL;
		}

//	sprintf(SQL_cmd,"create table if not exists %s([context] TEXT,[sw_code] INTEGER PRIMARY KEY, [xj_code] INTEGER);",TABLE_YXCODE);
	sprintf(SQL_cmd,"create table if not exists %s([context] TEXT,[sw_code] INTEGER PRIMARY KEY, [xj_code] TEXT);",TABLE_YXCODE);
	if (sqlite3_exec(code_db, SQL_cmd, 0, 0, &pErrMsg) != SQLITE_OK) {
		fprintf(stderr,"##libdatabase## Created tcucode.db database fail: %s\n",pErrMsg);
		sqlite3_free(pErrMsg);
		sqlite3_close(code_db);
		code_db = NULL;

		return ERROR_FAIL;
	}

//	sprintf(SQL_cmd,"create table if not exists %s([context] TEXT,[sw_code] INTEGER PRIMARY KEY, [xj_code] INTEGER);",TABLE_STOPREASON);
	sprintf(SQL_cmd,"create table if not exists %s([context] TEXT,[sw_code] INTEGER PRIMARY KEY, [xj_code] TEXT);",TABLE_STOPREASON);
	if (sqlite3_exec(code_db, SQL_cmd, 0, 0, &pErrMsg) != SQLITE_OK) {
		fprintf(stderr,"##libdatabase## Created tcucode.db database fail: %s\n",pErrMsg);
		sqlite3_free(pErrMsg);
		sqlite3_close(code_db);
		code_db = NULL;

		return ERROR_FAIL;
	}


	sqlite3_free(pErrMsg);
	printf("##libdatabase## Created tcucode.db success!\n");

	return ERROR_OK;

}

static  int insert_code_info(char* tableName,Display_Code* pcode)
{
	char *pErrMsg = NULL;
	char SQL_cmd[512] = { 0 };

	if(!tableName || !pcode){
		printf("##libdatabase## %s error, due to  para is NULL!\n",__func__);
		return ERROR_INPARA;
	}

	if(!pcode->context)
		memcpy(pcode->context,"NULL",5);

//	sprintf(SQL_cmd, "insert into %s values(\'%s\',%d,%d);", tableName,pcode->context,pcode->sw_code,pcode->xj_code);
	sprintf(SQL_cmd, "insert into %s values(\'%s\',%d,\'%04X\');", tableName,pcode->context,pcode->sw_code,pcode->xj_code);
	if (sqlite3_exec(code_db, SQL_cmd, 0, 0, &pErrMsg) != SQLITE_OK) {
		fprintf(stderr,"##libdatabase## insert tcucode.db fail! reason:%s \n",pErrMsg);
		sqlite3_free(pErrMsg);
		sqlite3_close(code_db);
		code_db = NULL;
		return ERROR_FAIL;
	}

	printf("##libdatabase## insert tcucode.db success!\n");

	sprintf(SQL_cmd, "select * from %s where sw_code=%d;",tableName,pcode->sw_code);
	sqlite3_exec(code_db, SQL_cmd, callback_func, 0, &pErrMsg);
	sqlite3_free(pErrMsg);

	return ERROR_OK;
}

static  int update_code_info(char* tableName, Display_Code* pcode)
{
	char *pErrMsg = NULL;
	char SQL_cmd[512] = { 0 };

	if(!tableName || !pcode){
		printf("##libdatabase## %s error, due to  para is NULL!\n",__func__);
		return ERROR_INPARA;
	}

	sprintf(SQL_cmd,"update %s set xj_code=\'%04X\' where sw_code=%d;",tableName, pcode->xj_code,pcode->sw_code);
	if (sqlite3_exec(code_db, SQL_cmd, 0, 0, &pErrMsg) != SQLITE_OK) {
		fprintf(stderr,"##libdatabase## update  tcucode.db fail! reason:%s\n",pErrMsg);
		sqlite3_free(pErrMsg);
		sqlite3_close(code_db);
		code_db = NULL;
		return ERROR_FAIL;
	}

	printf("##libdatabase## update code_db.db success! sw_code:%d  xj_code:%x\n",pcode->sw_code,pcode->xj_code);
	sqlite3_free(pErrMsg);

	return ERROR_OK;
}

static  int delete_code_info(char* tableName, Display_Code* pcode)
{
	char *pErrMsg = NULL;
	char SQL_cmd[512] = { 0 };

	if(!tableName || !pcode){
		printf("##libdatabase## %s error, due to  para is NULL!\n",__func__);
		return ERROR_INPARA;
	}
	sprintf(SQL_cmd, "delete from %s where sw_code=%d",tableName,pcode->sw_code);
	printf("%s\n",SQL_cmd);
	if (sqlite3_exec(code_db, SQL_cmd, 0, 0, &pErrMsg) != SQLITE_OK) {
		fprintf(stderr,"##libdatabase## delete from %s fail!,reason:%s\n",tableName,pErrMsg);
		sqlite3_free(pErrMsg);
		sqlite3_close(code_db);
		code_db = NULL;

		return ERROR_FAIL;
	}

	printf("##libdatabase## delete from %s success!, sw_code:%d\n",tableName,pcode->sw_code);

	return ERROR_OK;
}

static  int set_code_value(int *ncol, int *index, char **pResult, Display_Code* pcode)
{
	if (!ncol || !index || !pResult || !pcode)
		return ERROR_INPARA;

	char tmp[5]={0};
	int i = 0, len=0;
	unsigned int value=0, ret=0, offset=0;

	memcpy(pcode->context,pResult[(*index) + CODE_CONTEXT],CONTEXT_LEN);
	pcode->sw_code = atoi(pResult[(*index) + CODE_SWCODE]);
//	pcode->xj_code = atoi(pResult[(*index) + CODE_XJCODE]);

	strncpy(tmp,(pResult[(*index) + CODE_XJCODE]), 4);
	len = strlen(tmp);
	for(i=0; i<len; i++){
		ret = atoh(tmp[i]);
		offset = 4*(len-i-1);
		value |= (ret<<offset);
	}
	pcode->xj_code=value;

	(*index) = (*index) + (*ncol);
	return ERROR_OK;
}

static  void create_default_codetable()
{
	int i = 0;
	while(strcmp(Table_YX[i].context,"NULL")){
		insert_code_info(TABLE_YXCODE,&Table_YX[i]);
		i++;
	}

	i=0;
	while(strcmp(Table_StopReason[i].context,"NULL")){
		insert_code_info(TABLE_STOPREASON,&Table_StopReason[i]);
		i++;
	}
}

int protocol_get_YxCode(unsigned int swcode, Display_Code* pcode){
	int ret = 0;
	char* pErrMsg = NULL;
	char SQL_cmd[128] = { 0 };
	int nrow = 0, ncolnum = 0 ,index=0;
	char **pResult = NULL;
	Display_Code code;

	if(!pcode){
		printf("##libdatabase##%s fail, due to para is NULL!\n",__func__);
		return ERROR_INPARA;
	}

	ret = file_is_exist(CODE_PATH, CODE_DB_TYPE);
	if (ret == ERROR_INPARA)
		return ERROR_FAIL;
	else if (ret == ERROR_FAIL) {
		if (create_displaycode_db() != ERROR_OK)
			return ERROR_FAIL;
		create_default_codetable();
	}

	if (code_db == NULL) {
		if (sqlite3_open(CODE_PATH, &code_db) != SQLITE_OK) {
			sqlite3_close(code_db);
			code_db = NULL;

			return ERROR_FAIL;
		}
	}

	sprintf(SQL_cmd, "select * from %s where sw_code=%d;",TABLE_YXCODE, swcode);

	if (sqlite3_get_table(code_db, SQL_cmd, &pResult, &nrow, &ncolnum,&pErrMsg) != SQLITE_OK) {
		printf("##libdatabase## %s tcucode.db get table info fail!--->%s\n",__func__,pErrMsg);
		sqlite3_free(pErrMsg);
		sqlite3_free_table(pResult);
		sqlite3_close(guninfo_db);
		code_db = NULL;
		printf("##libdatabase## tcucode.db get yxcode(sw_code:%d) info  failed!, due to query databse failed!\n",swcode);
		return ERROR_FAIL;
	}

	if (nrow == 0)
		return ERROR_FAIL;

	index = ncolnum;
	memset(&code,0,sizeof(Display_Code));
	if (set_code_value(&ncolnum, &index, pResult, &code) != ERROR_OK) {
		sqlite3_free(pErrMsg);
		sqlite3_free_table(pResult);
		printf("##libdatabase## tcucode.db get yxcode(sw_code:%d) info  failed!, due to set value error!\n",swcode);
		return ERROR_FAIL;
	}

	sqlite3_free(pErrMsg);
	sqlite3_free_table(pResult);

	memcpy(pcode,&code,sizeof(Display_Code));
	//	printf("##libdatabase## get yxcode(sw_code:%d) info  success!\n",pcode->sw_code);

	return ERROR_OK;
}

int protocol_get_StopReasonCode(unsigned int swcode, Display_Code* pcode){
	int ret = 0;
	char* pErrMsg = NULL;
	char SQL_cmd[128] = { 0 };
	int nrow = 0, ncolnum = 0 ,index=0;
	char **pResult = NULL;
	Display_Code code;

	if(!pcode){
		printf("##libdatabase##%s fail, due to para is NULL!\n",__func__);
		return ERROR_INPARA;
	}

	ret = file_is_exist(CODE_PATH, CODE_DB_TYPE);
	if (ret == ERROR_INPARA)
		return ERROR_FAIL;
	else if (ret == ERROR_FAIL) {
		if (create_displaycode_db() != ERROR_OK)
			return ERROR_FAIL;
		create_default_codetable();
	}

	if (code_db == NULL) {
		if (sqlite3_open(CODE_PATH, &code_db) != SQLITE_OK) {
			sqlite3_close(code_db);
			code_db = NULL;

			return ERROR_FAIL;
		}
	}

	sprintf(SQL_cmd, "select * from %s where sw_code=%d;",TABLE_STOPREASON, swcode);
	if (sqlite3_get_table(code_db, SQL_cmd, &pResult, &nrow, &ncolnum,&pErrMsg) != SQLITE_OK) {
		printf("##libdatabase## %s tcucode.db get table info fail!--->%s\n",__func__,pErrMsg);
		sqlite3_free(pErrMsg);
		sqlite3_free_table(pResult);
		sqlite3_close(guninfo_db);
		code_db = NULL;
		printf("##libdatabase## tcucode.db get yxcode(sw_code:%d) info  failed!, due to query databse failed!\n",swcode);
		return ERROR_FAIL;
	}

	if (nrow == 0)
		return ERROR_FAIL;

	index = ncolnum;
	memset(&code,0,sizeof(Display_Code));
	if (set_code_value(&ncolnum, &index, pResult, &code) != ERROR_OK) {
		sqlite3_free(pErrMsg);
		sqlite3_free_table(pResult);
		printf("##libdatabase## get yxcode(sw_code:%d) info  failed!, due to set value error!\n",swcode);
		return ERROR_FAIL;
	}

	sqlite3_free(pErrMsg);
	sqlite3_free_table(pResult);

	memcpy(pcode,&code,sizeof(Display_Code));
	//	printf("##libdatabase## tcucode.db get yxcode(sw_code:%d) info  success!\n",pcode->sw_code);

	return ERROR_OK;
}

int protocol_update_YxCode( Display_Code* pcode){
	int ret = 0;
	char* pErrMsg = NULL;
	char SQL_cmd[128] = { 0 };
	int nrow = 0, ncolnum = 0;
	char **pResult = NULL;

	if(!pcode){
		printf("##libdatabase##%s fail, due to para is NULL!\n",__func__);
		return ERROR_INPARA;
	}

	ret = file_is_exist(CODE_PATH, CODE_DB_TYPE);
	if (ret == ERROR_INPARA)
		return ERROR_FAIL;
	else if (ret == ERROR_FAIL) {
		if (create_displaycode_db() != ERROR_OK)
			return ERROR_FAIL;
		create_default_codetable();
	}

	if (code_db == NULL) {
		if (sqlite3_open(CODE_PATH, &code_db) != SQLITE_OK) {
			sqlite3_close(code_db);
			code_db = NULL;

			return ERROR_FAIL;
		}
	}

	sprintf(SQL_cmd, "select * from %s where sw_code=%d;",TABLE_YXCODE,pcode->sw_code);
	if (sqlite3_get_table(code_db, SQL_cmd, &pResult, &nrow, &ncolnum,&pErrMsg) != SQLITE_OK) {
		printf("##libdatabase## update tcucode.db yxcode get table info fail!--->%s\n",pErrMsg);
		sqlite3_free(pErrMsg);
		sqlite3_free_table(pResult);
		sqlite3_close(code_db);
		code_db = NULL;

		return ERROR_FAIL;
	}

	if (nrow == 0) {
		if(strlen(pcode->context) == 0)
			strcpy(pcode->context,"NULL Context");
		ret = insert_code_info(TABLE_YXCODE,pcode);
	} else if (nrow == 1) {
		ret = update_code_info(TABLE_YXCODE,pcode);
	} else {
		ret = delete_code_info(TABLE_YXCODE,pcode);
		if(ret == ERROR_OK)
			ret = insert_code_info(TABLE_YXCODE,pcode);
	}

	if(ret == ERROR_OK){
		sqlite3_exec(code_db, SQL_cmd, callback_func, 0, &pErrMsg);
	}

	sqlite3_free(pErrMsg);
	sqlite3_free_table(pResult);

	return ERROR_OK;
}

int protocol_update_StopReasonCode(Display_Code* pcode){
	int ret = 0;
	char* pErrMsg = NULL;
	char SQL_cmd[128] = { 0 };
	int nrow = 0, ncolnum = 0;
	char **pResult = NULL;

	if(!pcode){
		printf("##libdatabase##%s fail, due to para is NULL!\n",__func__);
		return ERROR_INPARA;
	}

	ret = file_is_exist(CODE_PATH, CODE_DB_TYPE);
	if (ret == ERROR_INPARA)
		return ERROR_FAIL;
	else if (ret == ERROR_FAIL) {
		if (create_displaycode_db() != ERROR_OK)
			return ERROR_FAIL;
		create_default_codetable();
	}

	if (code_db == NULL) {
		if (sqlite3_open(CODE_PATH, &code_db) != SQLITE_OK) {
			sqlite3_close(code_db);
			code_db = NULL;

			return ERROR_FAIL;
		}
	}

	sprintf(SQL_cmd, "select * from %s where sw_code=%d;",TABLE_STOPREASON,pcode->sw_code);
	if (sqlite3_get_table(code_db, SQL_cmd, &pResult, &nrow, &ncolnum,&pErrMsg) != SQLITE_OK) {
		printf("##libdatabase## update tcucode.db yxcode get table info fail!--->%s\n",pErrMsg);
		sqlite3_free(pErrMsg);
		sqlite3_free_table(pResult);
		sqlite3_close(code_db);
		code_db = NULL;

		return ERROR_FAIL;
	}

	if (nrow == 0) {
		if(strlen(pcode->context) == 0)
			strcpy(pcode->context,"NULL Context");
		ret = insert_code_info(TABLE_STOPREASON,pcode);
	} else if (nrow == 1) {
		ret = update_code_info(TABLE_STOPREASON,pcode);
	} else {
		ret = delete_code_info(TABLE_STOPREASON,pcode);
		if(ret == ERROR_OK)
			ret = insert_code_info(TABLE_STOPREASON,pcode);
	}

	if(ret == ERROR_OK){
		sqlite3_exec(code_db, SQL_cmd, callback_func, 0, &pErrMsg);
	}

	sqlite3_free(pErrMsg);
	sqlite3_free_table(pResult);

	return ERROR_OK;
}

int protocol_delete_YxCode(unsigned int swcode)
{
	Display_Code code;
	int ret = 0;

	ret = file_is_exist(CODE_PATH, CODE_DB_TYPE);
	if (ret == ERROR_INPARA)
		return ERROR_FAIL;
	else if (ret == ERROR_FAIL) {
		if (create_displaycode_db() != ERROR_OK)
			return ERROR_FAIL;
		create_default_codetable();
	}

	if (code_db == NULL) {
		if (sqlite3_open(CODE_PATH, &code_db) != SQLITE_OK) {
			sqlite3_close(code_db);
			code_db = NULL;

			return ERROR_FAIL;
		}
	}

	memset(&code,0,sizeof(Display_Code));
	code.sw_code = swcode;

	return delete_code_info(TABLE_YXCODE,&code);
}

int protocol_delete_StopReasonCode(unsigned int swcode)
{
	Display_Code code;
	int ret = 0;

	ret = file_is_exist(CODE_PATH, CODE_DB_TYPE);
	if (ret == ERROR_INPARA)
		return ERROR_FAIL;
	else if (ret == ERROR_FAIL) {
		if (create_displaycode_db() != ERROR_OK)
			return ERROR_FAIL;
		create_default_codetable();
	}

	if (code_db == NULL) {
		if (sqlite3_open(CODE_PATH, &code_db) != SQLITE_OK) {
			sqlite3_close(code_db);
			code_db = NULL;

			return ERROR_FAIL;
		}
	}

	memset(&code,0,sizeof(Display_Code));
	code.sw_code = swcode;

	return delete_code_info(TABLE_STOPREASON,&code);
}

static int create_default_price_model_data()
{
	char *pErrMsg = NULL;
	char SQL_cmd[2048] = {0};
	unsigned int value=10000;


	sprintf(SQL_cmd,"insert into %s values(%u, %u, %u, %u, %u, %u, %u, %u, %u, %u, %u, %u, %u, %u, %u"
			", %u, %u, %u, %u, %u, %u, %u, %u, %u, %u, %u, %u, %u, %u, %u, %u, %u, %u, %u, %u, %u, %u, %u, %u"
			", %u, %u, %u, %u, %u, %u, %u, %u, %u, %u);",TABLE_XJ_PRICE_MODEL,XJ_POWER_RATE_TYPE, value, value, value, value, value, value,
			value, value, value, value, value, value, value, value, value, value, value, value, value, value, value, value, value,
			value, value, value, value, value, value, value, value, value, value, value, value, value, value, value, value, value,
			value, value, value, value, value, value, value, value);

	if(sqlite3_exec(price_db,SQL_cmd,0,0,&pErrMsg) != SQLITE_OK) {
		printf("##libdatabase## create_default_price_model_data tcuprice.db fail--->%s\n",pErrMsg);
		sqlite3_free(pErrMsg);
		sqlite3_close(price_db);
		price_db = NULL;

		return ERROR_FAIL;
	}

	sprintf(SQL_cmd,"insert into %s values(%u, %u, %u, %u, %u, %u, %u, %u, %u, %u, %u, %u, %u, %u, %u"
			", %u, %u, %u, %u, %u, %u, %u, %u, %u, %u, %u, %u, %u, %u, %u, %u, %u, %u, %u, %u, %u, %u, %u, %u"
			", %u, %u, %u, %u, %u, %u, %u, %u, %u, %u);",TABLE_XJ_PRICE_MODEL,XJ_SERVICE_RATE_TYPE, value, value, value, value, value, value,
			value, value, value, value, value, value, value, value, value, value, value, value, value, value, value, value, value,
			value, value, value, value, value, value, value, value, value, value, value, value, value, value, value, value, value,
			value, value, value, value, value, value, value, value);

	if(sqlite3_exec(price_db,SQL_cmd,0,0,&pErrMsg) != SQLITE_OK) {
		printf("##libdatabase## create_default_price_model_data tcuprice.db fail--->%s\n",pErrMsg);
		sqlite3_free(pErrMsg);
		sqlite3_close(price_db);
		price_db = NULL;

		return ERROR_FAIL;
	}

	printf("##libdatabase## create_default_price_model_data tcuprice .db success! \n");
	sqlite3_free(pErrMsg);

	return ERROR_OK;
}

static int create_price_model_db(){
	DIR *dp = NULL;
	char *pErrMsg = NULL;
	char  SQL_cmd[1024] = {0};

	if ((dp = opendir(DB_DIR_PATH)) == NULL)
		if (mkdir(DB_DIR_PATH, S_IRWXU | S_IRWXG | S_IRWXO) != 0) {
			price_db = NULL;
			closedir(dp);
			return ERROR_FAIL;
		}
	closedir(dp);

	if (access(PRICE_MODE_PATH, F_OK) != 0) {
		if (creat(PRICE_MODE_PATH, O_WRONLY | O_TRUNC) == -1) {
			price_db = NULL;
			return ERROR_FAIL;
		}
		chmod(PRICE_MODE_PATH, S_IRWXU | S_IRWXG | S_IRWXO);
	}

	if (price_db == NULL)
		if (sqlite3_open(PRICE_MODE_PATH, &price_db) != SQLITE_OK) {
			sqlite3_close(price_db);
			price_db = NULL;

			return ERROR_FAIL;
		}

	if(dbType.db_tran_type==TABLE_XJ){
		sprintf(SQL_cmd,
				"create table if not exists %s ([type] INTEGER PRIMARY KEY, [rate0] INTEGER, [rate1] INTEGER, [rate2] INTEGER, [rate3] INTEGER"
				", [rate4] INTEGER, [rate5] INTEGER, [rate6] INTEGER, [rate7] INTEGER, [rate8] INTEGER"
				", [rate9] INTEGER, [rate10] INTEGER, [rate11] INTEGER, [rate12] INTEGER, [rate13] INTEGER"
				", [rate14] INTEGER, [rate15] INTEGER, [rate16] INTEGER, [rate17] INTEGER, [rate18] INTEGER"
				", [rate19] INTEGER, [rate20] INTEGER, [rate21] INTEGER, [rate22] INTEGER, [rate23] INTEGER"
				", [rate24] INTEGER, [rate25] INTEGER, [rate26] INTEGER, [rate27] INTEGER, [rate28] INTEGER"
				", [rate29] INTEGER, [rate30] INTEGER, [rate31] INTEGER, [rate32] INTEGER, [rate33] INTEGER"
				", [rate34] INTEGER, [rate35] INTEGER, [rate36] INTEGER, [rate37] INTEGER, [rate38] INTEGER"
				", [rate39] INTEGER, [rate40] INTEGER, [rate41] INTEGER, [rate42] INTEGER, [rate43] INTEGER"
				", [rate44] INTEGER, [rate45] INTEGER, [rate46] INTEGER, [rate47] INTEGER);",TABLE_XJ_PRICE_MODEL);

		if (sqlite3_exec(price_db, SQL_cmd, 0, 0, &pErrMsg) != SQLITE_OK) {
			fprintf(stderr,
					"##libdatabase## Created tcuprice.db database fail: %s\n",
					pErrMsg);
			sqlite3_free(pErrMsg);
			sqlite3_close(price_db);
			price_db=NULL;
			return ERROR_FAIL;
		}

		create_default_price_model_data();
	}

	sqlite3_free(pErrMsg);
	printf("##libdatabase## Created tcuprice.db success!\n");
	return ERROR_OK;
}

static int update_price_model_db(void* model){
	char *pErrMsg = NULL;
	char SQL_cmd[2048] = {0};
	//int i = 0;

	if(dbType.db_tran_type == TABLE_XJ){
		xj_price_model * pm = (xj_price_model*)model;

		sprintf(SQL_cmd,"update %s set rate0=%d, rate1=%u, rate2=%u, rate3=%u, rate4=%u"
				", rate5=%u, rate6=%u, rate7=%u, rate8=%u, rate9=%u, rate10=%u, rate11=%u"
				", rate12=%u, rate13=%u, rate14=%u, rate15=%u, rate16=%u, rate17=%u, rate18=%u"
				", rate19=%u, rate20=%u, rate21=%u, rate22=%u, rate23=%u, rate24=%u, rate25=%u"
				", rate26=%u, rate27=%u, rate28=%u, rate29=%u, rate30=%u, rate31=%u, rate32=%u"
				", rate33=%u, rate34=%u, rate35=%u, rate36=%u, rate37=%u, rate38=%u, rate39=%u"
				", rate40=%u, rate41=%u, rate42=%u, rate43=%u, rate44=%u, rate45=%u, rate46=%u"
				", rate47=%u  where type=%u;" ,TABLE_XJ_PRICE_MODEL, pm->power_rate[0], pm->power_rate[1], pm->power_rate[2], pm->power_rate[3],
				pm->power_rate[4], pm->power_rate[5], pm->power_rate[6], pm->power_rate[7], pm->power_rate[8], pm->power_rate[9], pm->power_rate[10],
				pm->power_rate[11], pm->power_rate[12], pm->power_rate[13], pm->power_rate[14], pm->power_rate[15], pm->power_rate[16], pm->power_rate[17],
				pm->power_rate[18], pm->power_rate[19], pm->power_rate[20], pm->power_rate[21], pm->power_rate[22], pm->power_rate[23], pm->power_rate[24],
				pm->power_rate[25], pm->power_rate[26], pm->power_rate[27], pm->power_rate[28], pm->power_rate[29], pm->power_rate[30], pm->power_rate[31],
				pm->power_rate[32], pm->power_rate[33], pm->power_rate[34], pm->power_rate[35], pm->power_rate[36], pm->power_rate[37], pm->power_rate[38],
				pm->power_rate[39], pm->power_rate[40], pm->power_rate[41], pm->power_rate[42], pm->power_rate[43], pm->power_rate[44], pm->power_rate[45],
				pm->power_rate[46], pm->power_rate[47],XJ_POWER_RATE_TYPE);

		if(sqlite3_exec(price_db,SQL_cmd,0,0,&pErrMsg) != SQLITE_OK) {
			printf("##libdatabase## update tcuprice.db fail--->%s\n",pErrMsg);
			sqlite3_free(pErrMsg);
			sqlite3_close(price_db);
			price_db = NULL;

			return ERROR_FAIL;
		}

		sprintf(SQL_cmd,"update %s set rate0=%d, rate1=%u, rate2=%u, rate3=%u, rate4=%u"
				", rate5=%u, rate6=%u, rate7=%u, rate8=%u, rate9=%u, rate10=%u, rate11=%u"
				", rate12=%u, rate13=%u, rate14=%u, rate15=%u, rate16=%u, rate17=%u, rate18=%u"
				", rate19=%u, rate20=%u, rate21=%u, rate22=%u, rate23=%u, rate24=%u, rate25=%u"
				", rate26=%u, rate27=%u, rate28=%u, rate29=%u, rate30=%u, rate31=%u, rate32=%u"
				", rate33=%u, rate34=%u, rate35=%u, rate36=%u, rate37=%u, rate38=%u, rate39=%u"
				", rate40=%u, rate41=%u, rate42=%u, rate43=%u, rate44=%u, rate45=%u, rate46=%u"
				", rate47=%u  where type=%u;" ,TABLE_XJ_PRICE_MODEL, pm->service_rate[0], pm->service_rate[1], pm->service_rate[2], pm->service_rate[3],
				pm->service_rate[4], pm->service_rate[5], pm->service_rate[6], pm->service_rate[7], pm->service_rate[8], pm->service_rate[9], pm->service_rate[10],
				pm->service_rate[11], pm->service_rate[12], pm->service_rate[13], pm->service_rate[14], pm->service_rate[15], pm->service_rate[16], pm->service_rate[17],
				pm->service_rate[18], pm->service_rate[19], pm->service_rate[20], pm->service_rate[21], pm->service_rate[22], pm->service_rate[23], pm->service_rate[24],
				pm->service_rate[25], pm->service_rate[26], pm->service_rate[27], pm->service_rate[28], pm->service_rate[29], pm->service_rate[30], pm->service_rate[31],
				pm->service_rate[32], pm->service_rate[33], pm->service_rate[34], pm->service_rate[35], pm->service_rate[36], pm->service_rate[37], pm->service_rate[38],
				pm->service_rate[39], pm->service_rate[40], pm->service_rate[41], pm->service_rate[42], pm->service_rate[43], pm->service_rate[44], pm->service_rate[45],
				pm->service_rate[46], pm->service_rate[47],XJ_SERVICE_RATE_TYPE);

		if(sqlite3_exec(price_db,SQL_cmd,0,0,&pErrMsg) != SQLITE_OK) {
			printf("##libdatabase## update tcuprice.db fail--->%s\n",pErrMsg);
			sqlite3_free(pErrMsg);
			sqlite3_close(price_db);
			price_db = NULL;

			return ERROR_FAIL;
		}

		sprintf(SQL_cmd,"select * from %s;",TABLE_XJ_PRICE_MODEL);
		sqlite3_exec(price_db,SQL_cmd,callback_func,0,&pErrMsg);
		sqlite3_free(pErrMsg);
		printf("##libdatabase## update tcuprice.db success! \n");
	}

	return ERROR_OK;
}



static int set_xj_pm_value(int *ncol, int *index, char **pResult, xj_price_model *mode, int type){
	int i= 0;
	int len = 0;

	if(type==XJ_POWER_RATE_TYPE){
		len = sizeof(mode->power_rate)/sizeof(unsigned int);
		for(i=0; i<len; i++){
			mode->power_rate[i]=(unsigned int)atoi(pResult[(*index) + i+1]);
		}
	}
	else if(type==XJ_SERVICE_RATE_TYPE){
		len = sizeof(mode->service_rate)/sizeof(unsigned int);
		for(i=0; i<len; i++){
			mode->service_rate[i]=(unsigned int)atoi(pResult[(*index) + i + 1]);
		}
	}
	else
		return ERROR_FAIL;

	return ERROR_OK;
}

int protocol_get_PriceModel(void* model)
{
	char *pErrMsg = NULL;
	char SQL_cmd[64] = {0};
	int nrow = 0, ncol = 0;
	char **pResult = NULL;
	int index = 0;
	int i=0,len=0;

	if(!model)
		return ERROR_INPARA;

	if(file_is_exist(PRICE_MODE_PATH,PRICE_DB_TYPE) != ERROR_OK)
		if(create_price_model_db() != ERROR_OK)
			return ERROR_FAIL;

	if(price_db == NULL) {
		if(sqlite3_open(PRICE_MODE_PATH,&price_db) != SQLITE_OK) {
			sqlite3_close(price_db);
			price_db = NULL;

			return ERROR_FAIL;
		}
	}

	if(dbType.db_tran_type == TABLE_XJ){
		xj_price_model* pm= (xj_price_model*)model;

		sprintf(SQL_cmd,"select * from %s  where type=%d;",TABLE_XJ_PRICE_MODEL,XJ_POWER_RATE_TYPE);
		if(sqlite3_get_table(price_db,SQL_cmd,&pResult,&nrow,&ncol,&pErrMsg) == SQLITE_OK) {
			if(nrow == 0){//创建默认参数，并返回默认参数
				len = sizeof(pm->power_rate)/sizeof(unsigned int);
				for(i=0; i<len; i++){
					pm->power_rate[i]=10000;
				}
			}
			else{
				index = ncol;
				if(set_xj_pm_value(&ncol,&index,pResult,pm,XJ_POWER_RATE_TYPE) != ERROR_OK) {
					sqlite3_free(pErrMsg);
					sqlite3_free_table(pResult);
					return ERROR_FAIL;
				}
			}
		}
		else {
			printf("##libdatabase## query tcuprice.db get table info fail--->%s\n",pErrMsg);
			sqlite3_free(pErrMsg);
			sqlite3_free_table(pResult);
			sqlite3_close(price_db);
			price_db = NULL;
			return ERROR_FAIL;
		}

		sprintf(SQL_cmd,"select * from %s  where type=%d;",TABLE_XJ_PRICE_MODEL,XJ_SERVICE_RATE_TYPE);
				if(sqlite3_get_table(price_db,SQL_cmd,&pResult,&nrow,&ncol,&pErrMsg) == SQLITE_OK) {
					if(nrow == 0){//创建默认参数，并返回默认参数
						len = sizeof(pm->service_rate)/sizeof(unsigned int);
						for(i=0; i<len; i++){
							pm->service_rate[i]=10000;
						}
					}
					else{
						index = ncol;
						if(set_xj_pm_value(&ncol,&index,pResult,pm,XJ_SERVICE_RATE_TYPE) != ERROR_OK) {
							sqlite3_free(pErrMsg);
							sqlite3_free_table(pResult);
							return ERROR_FAIL;
						}
					}
				}
				else {
					printf("##libdatabase## query tcuprice.db get table info fail--->%s\n",pErrMsg);
					sqlite3_free(pErrMsg);
					sqlite3_free_table(pResult);
					sqlite3_close(price_db);
					price_db = NULL;
					return ERROR_FAIL;
				}
	}
	else{
		printf("##libdatabase## %s Fail, No Support dbType, dbType.db_tran_type=%d\n",__func__,dbType.db_tran_type);
		return ERROR_FAIL;
	}


	sqlite3_free(pErrMsg);
	sqlite3_free_table(pResult);
	printf("##libdatabase## protocol_get_PriceModel tcuprice.db success!\n");

	return ERROR_OK;
}

int protocol_update_PriceModel(void* model){
	int ret = 0;

	if(!model)
		return ERROR_INPARA;

	ret = file_is_exist(PRICE_MODE_PATH,PRICE_DB_TYPE);
	if(ret == ERROR_INPARA)
		return ERROR_FAIL;
	else if(ret == ERROR_FAIL) {
		if(create_price_model_db() != ERROR_OK)
			return ERROR_FAIL;
	}

	if(price_db == NULL) {
		if(sqlite3_open(PRICE_MODE_PATH,&price_db) != SQLITE_OK) {
			sqlite3_close(price_db);
			price_db = NULL;

			return ERROR_FAIL;
		}
	}

	ret = update_price_model_db(model);


	return ret;
}



